Ich habe eine einfache Klasse (zu Testzwecken), die ich versuche, die Verwendung von JXPath abzufragen.Verwenden von JXPath zum Abfragen einer Liste
ich eine Liste von verschiedenen Tieren Objekten, und ich möchte für einen Iterator bekommen:
alle Tiere in dem type = 'CAT'
Alle Tiere, wo numLegs = 4
Hier
ist die einfache Klasse:
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;
}
}
Hier ist die Methode, die ich eine Liste der Tiere zu bauen bin mit mir gegen Abfrage:
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;
}
Hier ist, wie ich die Abfrage auszuführen versuchen:
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());
}
Dieser Teil:
Iterator<BeanPointer> iter =
animsContext.iteratePointers("/*[type='CAT']");
funktioniert nicht. Was mache ich falsch? Ich kann es auch nicht für/* [numLegs = 4] verwenden.