Jxpath provides tools for manipulating JavaBeans that conform to the Java class naming conventions using Xpath syntax. Maps, DOM, and other object models are also supported. For the JavaBean of deep structure, using Jxpath can easily access deep-seated attributes without tedious getter/setter operation.
Take the following JavaBeans as an example.
PackageCom.huey.jxpath;ImportLombok. Allargsconstructor;ImportLombok. Data;ImportLombok. Noargsconstructor, @Data @noargsconstructor@allargsconstructor Public classBook {PrivateString title; Privateauthor[] authors; PrivatePublisher publisher; PrivateString ISBN; Private DoublePrice ; }
Book.java
PackageCom.huey.jxpath;Importjava.util.Date;ImportLombok. Allargsconstructor;ImportLombok. Data;ImportLombok. Noargsconstructor, @Data @noargsconstructor@allargsconstructor Public classAuthor {PrivateString FirstName; PrivateString LastName; Private Chargender; PrivateDate birthday; }
Author.java
PackageCom.huey.jxpath;ImportJava.util.Map;ImportLombok. Allargsconstructor;ImportLombok. Data;ImportLombok. Noargsconstructor, @Data @noargsconstructor@allargsconstructor Public classPublisher {PrivateString name; PrivateString address; PrivateMap<string, string>contacts; }
Publisher.java
Initialization
author[] authors; Publisher publisher; Book Book;authors=Newauthor[] {NewAuthor ("Eric", "Freeman", ' F ',NewDate ()),NewAuthor ("Elelisabeth", "Freeman", ' M ',NewDate ())}; Map<string, string> contacts =NewHashmap<string, string>(); Contacts.put ("Tel", "010-12345678"); Contacts.put ("Fax", "010-87654321"); Contacts.put ("Email", "[email protected]");p ublisher=NewPublisher ("China Power Press", "Z of Yy Road, xx District, Beijing", contacts); Book=NewBook ("Head First Design Patterns", authors, publisher, "9787508353937", 98.0);
JavaBean Property Access
Jxpathcontext context == (String) context.getvalue ("title"= (Publisher) context.getvalue (" Publisher ");
Lenient Mode
The Context.getvalue (XPath) method throws an exception when the supplied XPath cannot be mapped to a JavaBean property, and returns Null if the method Context.setlenient (true) is called.
Nested Bean Property Access
Jxpathcontext context == (String) context.getvalue ("Publisher/name"); char agender = (Character) context.getvalue ("Authors[firstname= ' Eric ']/gender");
Collection subscripts
Jxpathcontext context == (Author) context.getvalue ("authors[1]"); // The subscript for the first element of a collection is 1 instead of 0
Retrieving multiple Results
Jxpathcontext context = jxpathcontext.newcontext (book); Iterator<?> authors = context.iterate ("Authors "); while (Authors.hasnext ()) { = (Author) Authors.next (); // ...}
Map Element Access
Jxpathcontext context = Jxpathcontext.newcontext (publisher); = (String) context.getvalue ("Contacts/tel"); = (String) context.getvalue ("contacts[@name = ' Fax ']");
Jxpath (1.3)-Object Graph traversal