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 parent node selection using .. operator.
/**
* @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 instance as ROOT node
JXPathContext context = JXPathContext.newContext(bookStore);
//Get all book titles by author 'C'
Iterator<?> bookNamesByC = context.iterate("/books/author[name='C']/../title"); while (bookNamesByC.hasNext()) {
System.out.println(bookNamesByC.next());
}
//Get all book titles by author 'B'
Iterator<?> bookNamesByB = context.iterate("/books/author[name='B']/../title"); while (bookNamesByB.hasNext()) {
System.out.println(bookNamesByB.next());
}