JXPathContext聽context聽=聽JXPathContext.newContext( obj );
//鍜?xpath 鐨?鑼冨洿紜畾
涓鑸彇鍊?瀛樺?
String聽fName聽=聽(String)context.getValue("firstName"); //setValue
//鍙傝?http://www.tkk7.com/Good-Game/archive/2007/08/10/135739.html
涓鑸殑緇熻鍜屼嬌鐢? c 涓?list [id,name,.....]
聽聽聽聽聽聽聽聽JXPathContext聽context聽=聽JXPathContext.newContext(c);
聽聽聽聽聽聽聽 System.out.println(聽context.getValue("count(聽.[name='oo'聽and聽id='1'聽]聽)")聽); //瀵硅薄 name=oo 鍜?id=1鐨勬湁澶氬皯涓?/font>
System.out.println( context.getValue("sum( .[name='oo' and id='1' ]/id )") );//瀵硅薄name=oo鍜宨d=1鐨勬墍鏈塱d鐩稿姞
寰楀埌闆嗗悎
聽Iterator聽threeBooks聽=聽context.iterate("books[position()聽<聽4]");
//xpath 鐨勪綅緗嚱鏁?position 鍏朵粬鍑芥暟鍙傝?http://www.w3.org/TR/xpath
//4 Core Function Library
xpath 浣跨敤
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");
//浣跨敤鐨勬槸 addressMap map 鐨?key = home 鐨凙ddress綾誨睘鎬х殑 zipCode
xml 鍦ㄧ▼搴?涓?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");
// 綾籆ontainer鐨?灞炴?locations 澶?vendor(xml鍐? .....
寤虹珛 Path宸ュ巶 灝辨槸 鑷畾涔夊瓧絎︿覆 寰楀埌 鑷畾涔夌被
聽public聽class聽AddressFactory聽extends聽AbstractFactory聽{
聽聽聽聽public聽boolean聽createObject(JXPathContext聽context,聽Pointer聽pointer,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Object聽parent,聽String聽name,聽int聽index){
聽聽聽聽聽if聽((parent聽instanceof聽Employee)聽&&聽name.equals("address"){
聽聽聽聽聽聽聽((Employee)parent).setAddress(new聽Address());
聽聽聽聽聽聽聽return聽true;
聽聽聽聽聽}
聽聽聽聽聽return聽false;
聽聽聽}
聽}
聽JXPathContext聽context聽=聽JXPathContext.newContext(emp);
聽context.setFactory(new聽AddressFactory());
聽context.createPath("address");
聽context.createPathAndSetValue("address/zipCode",聽"90190");
// emp 綾誨氨鏄?createObject鏂規(guī)硶涓殑 Object
//榪愯瑙f瀽鍒?address瀛楃 灝辮繘鍏?if涓?/font>
寤虹珛鍐呭弬
聽JXPathContext聽context聽=聽JXPathContext.newContext(auth);
聽context.getVariables().declareVariable("index",聽new聽Integer(2));
context.setValue("$index", new Integer(3));
聽Book聽secondBook聽=聽(Book)context.getValue("books[$index]");
// $index 涓?3
紜畾鑼冨洿 Pointer聽 JXPathContext聽context聽=聽JXPathContext.newContext(bean);
聽Pointer聽addressPtr聽=聽context.getPointer("/employees[1]/addresses[2]");
聽JXPathContext聽relativeContext聽=聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽context.getRelativeContext(addressPtr);
聽String聽zipCode聽=聽(String)relativeContext.getValue("zipCode");
//鍙互鐢?xpath 紜畾鑼冨洿 寰堝ソ 鍛靛懙
鏂規(guī)硶鐨勮仈緋誨簲鐢?br />聽public聽class聽Formats聽{
聽聽聽聽public聽static聽String聽date(Date聽d,聽String聽pattern){
聽聽聽聽聽聽聽聽return聽new聽SimpleDateFormat(pattern).format(d);
聽聽聽聽}
聽聽聽聽
聽}聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 聽
聽context.setFunctions(new聽ClassFunctions(Formats.class,聽"format"));
//鏂規(guī)硶鐨勮緗?format
聽
聽context.getVariables().declareVariable("today",聽new聽Date());
聽String聽today聽=
聽聽聽聽聽(String)context.getValue("format:date($today,聽'MM/dd/yyyy')");
蹇?jī)寰楀Q?浠g爜鍙互鍐欐垚浠涔堟牱鍛~ (JXpath)