Jxpath (1.3)-Object Graph traversal

Source: Internet
Author: User
Tags xpath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.