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 multiple predicates in JXPath expression.
/**
* @param args
*/ public static void main(String[] args) {
BookStore bookStore = new BookStore(
Arrays.asList( new Book("A-Book1", 11.11, new Author("A")), new Book("B-Book1", 22.22, new Author("B")), new Book("B-Book2", 33.33, new Author("B")), new Book("C-Book1", 33.33, new Author("C")), new Book("C-Book2", 16.66, new Author("C")), new Book("C-Book3", 48.12, new Author("C")), new Book("D-Book1", 44.44, new Author("D"))
)
);
//Create JXPathContext with BookStore object as ROOT node
JXPathContext context = JXPathContext.newContext(bookStore);
//Get books names by Author C and price > 30
Iterator<?> bookNamesByC = context.iterate("/books[price > 30]/author[name='C']/../title"); while (bookNamesByC.hasNext()) {
System.out.println(bookNamesByC.next());
}
//Get books by Author C and price < 40
Iterator<?> bookByC = context.iterate("/books[price < 40]/author[name='C']/.."); while (bookByC.hasNext()) {
System.out.println(bookByC.next());
}
}