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);
System.out.println( context.getValue("count( .[name='oo' and id='1' ] )") ); //對象 name=oo 和 id=1的有多少個
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 {
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 的Address類屬性的 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");
// 類Container的 屬性 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方法中的 Object
//運行解析到 address字符 就進入 if中
建立內參
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
確定范圍
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 確定范圍 很好 呵呵
方法的聯系應用
public class Formats {
public static String date(Date d, String pattern){
return new SimpleDateFormat(pattern).format(d);
}
...
}
context.setFunctions(new ClassFunctions(Formats.class, "format"));
//方法的設置 format
...
context.getVariables().declareVariable("today", new Date());
String today =
(String)context.getValue("format:date($today, 'MM/dd/yyyy')");
心得: 代碼可以寫成什麼樣呢~~ (JXpath)