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 exception handling in JXPath.
If the given XPath doesn't exist JXPath throws JXPathNotFoundException.
We can change this behavior by enabling lenient flag.
If the context is in the lenient mode, then getValue() returns null for inexistent paths.
/**
* @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 instance as ROOT node
JXPathContext context = JXPathContext.newContext(bookStore);
//Get the book with title 'ABCD' try {
System.out.println(context.getValue("/books[@title='ABCD']"));
} catch (JXPathNotFoundException e) {
System.out.println(e);
}