<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 165, comments - 198, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    jxpath 學習筆記

    Posted on 2007-08-13 10:58 G_G 閱讀(2200) 評論(1)  編輯  收藏 所屬分類: Jakarta Commons
    get set 參考 BeanUtil 包 和 Xpath
    http://commons.apache.org/? 的 jxpath

    類的加載
    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的有多少個
    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

    確定范圍
    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 確定范圍 很好 呵呵


    方法的聯系應用
    ?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)


    評論

    # re: jxpath 學習筆記  回復  更多評論   

    2007-09-05 17:26 by G_G
     1 2 3
     4 5 6
     7 8 9
     ...
     28 29 30




    結果集


     sum( .[1]/num1 ) 會全加 不會就第一個 ?
     sum( .[num1=1]/*[contains(name(),'num')] ) 模糊查詢使用  結果:6
     
     sum( .[num1=1]/*[contains(name(),'num')] ) + sum( .[num1=4]/*[contains(name(),'num')] ) 
     sum( .[num1=1 or num1=4]/*[contains(name(),'num')] )
     結果:21
     
     .[position()=last()]/num1  錯誤? 不支持 last()  等方法嗎?
     .[last()]/num1  結果 1
     .[last()-1]/num1 錯誤  jxpath 對定位好象不太兼容 。
     
     
     substring-after('1999/04/01','/') 結果04/01
     
     .[num1=1]/*[name()='num1']  結果:1
     
     last()  結果:10
     
      .[num1=4]/*[string-length( name() )
    <=4 ]  結果: 4 5 6 


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 色播在线永久免费视频网站| 免费在线观看的网站| 精品久久久久久国产免费了 | 久久免费动漫品精老司机| 免费成人黄色大片| 思思久久99热免费精品6| 午夜免费啪视频在线观看| 亚洲人成色777777在线观看| 久久嫩草影院免费看夜色| 亚洲中文字幕无码久久2017| 91成人免费观看在线观看| 亚洲精品无码精品mV在线观看| 中文字幕免费观看视频| 久久久久久久久亚洲| 久久免费区一区二区三波多野| 久久久亚洲欧洲日产国码aⅴ| 中国人xxxxx69免费视频| 免费观看a级毛片| 国产成人不卡亚洲精品91| 免费人成视网站在线观看不卡| www.av在线免费观看| 亚洲啪啪AV无码片| 三年片在线观看免费观看大全一| 亚洲综合色丁香麻豆| 国产vA免费精品高清在线观看| 亚洲欧洲无码AV电影在线观看| 免费h视频在线观看| 亚洲成人国产精品| 国产成人无码区免费网站| 久久亚洲私人国产精品vA| 毛色毛片免费观看| 污污的视频在线免费观看| 亚洲va久久久噜噜噜久久| 免费国产a理论片| 亚洲av片劲爆在线观看| 久久久www成人免费毛片| 四虎成人精品国产永久免费无码| 久久久久久亚洲精品| 国产精品无码一区二区三区免费 | 国产高清在线精品免费软件| 国产免费播放一区二区|