<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 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    jxpath 學(xué)習(xí)筆記

    Posted on 2007-08-13 10:58 G_G 閱讀(2192) 評(píng)論(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://m.tkk7.com/Good-Game/archive/2007/08/10/135739.html

    一般的統(tǒng)計(jì)和使用 c 為 list [id,name,.....]

    ????????JXPathContext?context?=?JXPathContext.newContext(c);
    ??????? System.out.println(?context.getValue(
    "count(?.[name='oo'?and?id='1'?]?)")?); //對(duì)象 name=oo 和 id=1的有多少個(gè)
    System.out.println( context.getValue("sum( .[name='oo' and id='1' ]/id )") );//對(duì)象name=oo和id=1的所有id相加





    得到集合
    ?Iterator?threeBooks?=?context.iterate("books[position()?<?4]");
    //xpath 的位置函數(shù) position 其他函數(shù)參考 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 的切入點(diǎn)
    ????<?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內(nèi)) .....

    建立 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
    //運(yùn)行解析到 address字符 就進(jìn)入 if中


    建立內(nèi)參
    ?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 確定范圍 很好 呵呵


    方法的聯(lián)系應(yīng)用
    ?public?class?Formats?{
    ????
    public?static?String?date(Date?d,?String?pattern){
    ????????
    return?new?SimpleDateFormat(pattern).format(d);
    ????}
    ????
    ?}??????????????????????????????????????????????????? ?
    ?context.setFunctions(
    new?ClassFunctions(Formats.class,?"format"));
    //方法的設(shè)置 format
    ?
    ?context.getVariables().declareVariable(
    "today",?new?Date());
    ?String?today?
    =
    ?????(String)context.getValue(
    "format:date($today,?'MM/dd/yyyy')");


    心得: 代碼可以寫成什么樣呢~~ (JXpath)


    評(píng)論

    # re: jxpath 學(xué)習(xí)筆記  回復(fù)  更多評(píng)論   

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




    結(jié)果集


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


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲免费人成在线视频观看 | 美女巨胸喷奶水视频www免费| 久久99国产亚洲高清观看首页| 日韩av无码久久精品免费| 亚洲av无码不卡久久| 四虎国产精品免费视| a级毛片免费完整视频| 丁香婷婷亚洲六月综合色| 亚洲国产成人久久综合野外| 免费观看91视频| 亚洲老熟女五十路老熟女bbw| 亚洲性日韩精品国产一区二区| 久久综合给合久久国产免费| 亚洲av无码成人精品区一本二本| 亚洲色精品aⅴ一区区三区| 国产91色综合久久免费| 一级一级毛片免费播放| 亚洲日韩在线视频| 精品国产亚洲男女在线线电影 | 亚洲七七久久精品中文国产| 91禁漫免费进入| 精品在线观看免费| 亚洲午夜免费视频| 亚洲黄黄黄网站在线观看| 久久免费看黄a级毛片| eeuss影院www天堂免费| 亚洲av乱码一区二区三区香蕉| 精品国产亚洲男女在线线电影| 影音先锋在线免费观看| 久久久久久国产精品免费免费男同| 蜜芽亚洲av无码一区二区三区| 色婷婷六月亚洲婷婷丁香| 亚洲第一页日韩专区| 欧美好看的免费电影在线观看| 免费看少妇高潮成人片| 日韩在线观看免费| 亚洲日韩一区精品射精| 亚洲伊人色一综合网| 久久亚洲精品成人| 久久亚洲2019中文字幕| 国产精品成人无码免费|