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 variable declaration in JXPath.
/**
* @param args
*/ public static void main(String[] args) {
BookStore bookStore = new BookStore(
Arrays.asList( new Book("A1", 11.11, new Author("X1")), new Book("B2", 22.22, new Author("X2")), new Book("C3", 33.33, new Author("X3")), new Book("D4", 44.44, new Author("X4"))
)
);
//Create JXPathContext with BookStore object as ROOT node
JXPathContext context = JXPathContext.newContext(bookStore);
//Declare a variable named 'book_title'
context.getVariables().declareVariable("book_title", "B2");
//Get the book with title 'B2'
System.out.println(context.getValue("/books[@title=$book_title]"));
//Get the price of book with title 'B2'
System.out.println(context.getValue("/books[@title=$book_title]/price"));
//Get the author name of book titled 'B2'
System.out.println(context.getValue("/books[@title=$book_title]/author/name"));
System.out.println(context.getValue("/books[@title=$book_title]/author/email"));
}