JXPath is a java library for Object Graph Navigation using the XPath syntax.
This requires the libraries commons-jxpath-1.3.jar, commons-beanutils.jar and commons-logging.jar to be in classpath.
The following example shows using count function in JXPath.
/**
* @param args
*/ public static void main(String[] args) {
Student std1 = new Student("Sriram", 2, "Chess", new String [] { "Sri", "Ram", "Bunny", "Sweety", "Honey"});
Student std2 = new Student("Sudhakar", 29, "Cricket", new String [] { "Venkat", "Munna", "Sudha" });
Student std3 = new Student("Anuradha", 2, "Cooking", new String [] { "Anu", "Radha" });
StudentClass stCls = new StudentClass("MPC");
stCls.setStudents(Arrays.asList(std1, std2, std3));
//Create JXPathContext with StudentClass instance as ROOT node
JXPathContext context = JXPathContext.newContext(stCls);
//Get students having more than 2 nick names
Iterator<?> students = context.iterate("/students[count(nickNames) > 2]"); while (students.hasNext()) {
System.out.println(students.next());
}
//Get students having 4 or less nick names
Iterator<?> students2 = context.iterate("/students[count(nickNames) <= 4]"); while (students2.hasNext()) {
System.out.println(students2.next());
}
}
}