JXPathを使用して照会しようとしている単純なクラス(テスト目的用)があります。JXPathを使用したリストの照会
私は様々な動物オブジェクトのリストを作成し、私はイテレータを取得したい:numLegs = 4
すべての動物すべての動物の種類は= 'CAT'ここで
は、単純なクラスです:
public class Animal {
private UUID uuid;
private int numLegs;
private AnimalType type;
public enum AnimalType {
CHICKEN, FROG, DOG, CAT
}
public Animal(AnimalType type) {
this.type = type;
uuid = UUID.randomUUID();
switch (type) {
case CHICKEN:
numLegs = 2;
break;
case FROG:
numLegs = 2;
break;
case DOG:
numLegs = 4;
break;
case CAT:
numLegs = 4;
break;
}
}
public UUID getUuid() {
return uuid;
}
public int getNumLegs() {
return numLegs;
}
public AnimalType getType() {
return type;
}
public String toString(){
return "UUID: "+uuid+", Animal: "+type+ ", Legs: "+numLegs;
}
}
0ここで私は私がに対する照会するために動物のリストを構築するために使用しています方法であって、ここで
private static List<Animal> getAnimals(int numAnimals) {
ArrayList<Animal> animals = new ArrayList<Animal>();
for(int i = 0; i<numAnimals; i++){
if(i%4==0){
animals.add(new Animal(AnimalType.CAT));
}
else if(i%3==0){
animals.add(new Animal(AnimalType.DOG));
}
else if(i%2==0){
animals.add(new Animal(AnimalType.FROG));
}
else{
animals.add(new Animal(AnimalType.CHICKEN));
}
}
return animals;
}
は、私は、クエリを実行しようとしている方法です:
public static void main(String[] args){
List<Animal> animals = getAnimals(10000);
JXPathContext animsContext = JXPathContext.newContext(animals);
Iterator<BeanPointer> iter =
animsContext.iteratePointers("/*[type='CAT']");
List<Animal> cats = new ArrayList<Animal>();
while(iter.hasNext()){
cats.add((Animal) iter.next().getParent().getValue());
}
System.out.println("Number of Cats is: "+cats.size());
}
この部分:
Iterator<BeanPointer> iter =
animsContext.iteratePointers("/*[type='CAT']");
は機能しません。私は間違って何をしていますか?私はそれを/ * [numLegs = 4]のどちらかで働かせることはできません。