jxpath学习笔记

get set 参考 BeanUtil 包 和 Xpath

http://commons.apache.org/ 的 jxpath User’s Guide

类的加载

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

一般取值 存值 

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

一般的统计和使用 c 为 list [id,name,…..]

JXPathContext context = JXPathContext.newContext(c);<br />System.out.println( context.getValue("count( .[name='oo' and id='1' ] )") ); //对象 name=oo 和 id=1的有多少个<br />System.out.println( context.getValue("sum( .[name='oo' and id='1' ]/id )") );//对象name=oo和id=1的所有id相加

得到集合

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

xpath 使用

public class Employee {<br />    private Map addressMap = new HashMap();<br />    {        addressMap.put("home", new Address(...));<br />        addressMap.put("office", new Address(...));<br />    }<br />    public Map getAddresses(){<br />      return addressMap;<br />    }<br />    ...<br />}<br /> String homeZipCode = (String)context. getValue("addresses[@name='home']/zipCode");<br />//使用的是 addressMap map 的 key = home 的Address类属性的 zipCode

xml 在程序 与 xpath 的切入点

<?xml version="1.0" ?><br />    <vendor><br />      <location id="store101"><br />        <address><br />          <street>Orchard Road</street><br />        </address>      </location>      <location id="store102">        <address>          <street>Tangerine Drive</street>        </address>      </location>    </vendor><br />class Company {    private Container locations = null;    public Container getLocations(){        if (locations == null){            URL url = getClass().getResource("Vendor.xml");            locations = new XMLDocumentContainer(url);        }        return locations;    }<br /> }  ...  context = JXPathContext.newContext(new Company());<br />  ...  String street = (String)context.getValue(                "locations/vendor/location[@id = 'store102']//street");<br />// 类Container的 属性 locations 头 vendor(xml内) .....

建立 Path工厂 就是 自定义字符串 得到 自定义类

public class AddressFactory extends AbstractFactory {<br />    public boolean createObject(JXPathContext context, Pointer pointer,                                Object parent, String name, int index){<br />     if ((parent instanceof Employee) && name.equals("address"){       ((Employee)parent).setAddress(new Address());       return true;     }     return false;   } } JXPathContext context = JXPathContext.newContext(emp);<br /> context.setFactory(new AddressFactory()); context.createPath("address");<br /> context.createPathAndSetValue("address/zipCode", "90190");<br />// emp 类就是 createObject方法中的 Object<br />//运行解析到 address字符 就进入 if中

建立内参

JXPathContext context = JXPathContext.newContext(auth);<br /> context.getVariables().declareVariable("index", new Integer(2));<br /> context.setValue("$index", new Integer(3)); Book secondBook = (Book)context.getValue("books[$index]");// $index 为 3

确定范围

JXPathContext context = JXPathContext.newContext(bean);<br /> Pointer addressPtr = context.getPointer("/employees[1]/addresses[2]");<br /> JXPathContext relativeContext =<br />               context.getRelativeContext(addressPtr); String zipCode = (String)relativeContext.getValue("zipCode");<br />//可以用 xpath 确定范围 很好 呵呵

方法的联系应用

public class Formats {<br />    public static String date(Date d, String pattern){<br />        return new SimpleDateFormat(pattern).format(d);<br />    }<br />    ...<br />}<br />context.setFunctions(new ClassFunctions(Formats.class, "format"));<br />//方法的设置 format<br />...<br />context.getVariables().declareVariable("today", new Date());<br /> String today =<br />     (String)context.getValue("format:date($today, 'MM/dd/yyyy')");

心得: 代码可以写成什么样呢~~ (JXpath)

要么读书、要么旅行,灵魂和身体,必须有一个在路上。

jxpath学习笔记

相关文章:

你感兴趣的文章:

标签云: