Jxpath Learning Notes

Source: Internet
Author: User

Get set reference Beanutil package and Xpath

http://commons.apache.org/'s Jxpath User ' s Guide

Loading of classes

JXPathContext context=JXPathContext.newContext( obj );
//和 xpath 的 范围确定

General value stored value

String fName=(String)context.getValue("firstName"); //setValue
//参考 http://www.blogjava.net/Good-Game/archive/2007/08/10/135739.html

General statistics and use of C for list [Id,name,.....]

Jxpathcontext context = Jxpathcontext.newcontext (c);
System.out.println (Context.getvalue ("count (. [ Name= ' oo ' and id= ' 1 ')); The number of objects Name=oo and id=1
System.out.println (Context.getvalue ("Sum" (). [ Name= ' oo ' and id= ' 1 ']/id))//Object Name=oo and id=1 all IDs are added

Get Set

Iterator threeBooks=context.iterate("books[position()<4]");
//xpath 的位置函数 position 其他函数参考 http://www.w3.org/TR/xpath 
//4 Core Function Library

XPath uses

public class Employee {
Private Map Addressmap = new HashMap ();
{ addressmap.put ("Home", new address (...));
Addressmap.put ("Office", new Address (...));
}
Public Map getaddresses () {
return addressmap;
}
...
}
String homezipcode = (string) context. GetValue ("addresses[@name = ' home ']/zipcode");
Using the Addressmap Map key = home Address class attribute ZipCode

The entry point of XML in program and XPath

<?xml version= "1.0"?>
<vendor>
<location id= "store101" >
<address>
<street>orchard road</street>
</address> </location> <location id= "store102" > <address> <street >tangerine drive</street> </address> </location> </vendor>
Class Company { private Container locations = null; Public Container getlocations () { if (locations = null) { URL url = getclass (). GetResource ("Vendor.xml"); locations = new Xmldocumentcontainer (URL); } return locations; }
} ... Context = Jxpathcontext.newcontext (new company ());
... String street = (string) context.getvalue ( "locations/vendor/location[@id = ' store102 ']//street");
Class Container Properties Locations header vendor (in XML) ...

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.