• 爪哇国新游记之三十四----Dom4j的XPath操作


    Dom4j是Java访问XML的利器之一,另一个是JDom。记得当年因为粗掌握点JDomAPI但项目要求使用Dom4j还闹一阵情绪,现在看来真是没必要,只花一些时间成本就进去一个新世界绝对是值得做的一件事。更何况JDom因无人更新而停顿了。

    Dom4j有两个包,一个是dom4j-1.6.1.jar,它提供基本的XML API支持,如访问节点,属性等。

    还有一个是jaxen-1.1-beta-9.jar,它提供XPath支持。

    关于XPath的语法,请见转载:XPath基本语法

    言归正传,下面请看例程。

    1.访问节点群

    XML样本:

    <applications>
        <application name='chat'>
            <mtLanguage source='ar_ar' target='en_us' />
            <mtLanguage source='zh_cn' target='en_us' />
            <mtLanguage source='zh_tw' target='en_us' />
            <mtLanguage source='en_us' target='ar_ar' />
            <mtLanguage source='en_us' target='zh_cn' />
            <mtLanguage source='en_us' target='zh_tw' />
            <mtLanguage source='en_us' target='fr_fr' />
            <mtLanguage source='en_us' target='de_de' />
            <mtLanguage source='en_us' target='it_it' />
            <mtLanguage source='en_us' target='ja_jp' />
            <mtLanguage source='en_us' target='ko_kr' />
            <mtLanguage source='en_us' target='pt_br' />
            <mtLanguage source='en_us' target='ru_ru' />
            <mtLanguage source='en_us' target='es_es' />
            <mtLanguage source='fr_fr' target='en_us' />
            <mtLanguage source='de_de' target='en_us' />
            <mtLanguage source='it_it' target='en_us' />
            <mtLanguage source='ja_jp' target='en_us' />
            <mtLanguage source='ko_kr' target='en_us' />
            <mtLanguage source='pt_br' target='en_us' />
            <mtLanguage source='ru_ru' target='en_us' />
            <mtLanguage source='es_es' target='en_us' />
        </application>
        <application name='doc'>
            <mtLanguage source='ar_ar' target='en_us' />
            <mtLanguage source='zh_cn' target='en_us' />
            <mtLanguage source='zh_tw' target='en_us' />
            <mtLanguage source='en_us' target='ar_ar' />
            <mtLanguage source='en_us' target='zh_cn' />
            <mtLanguage source='en_us' target='zh_tw' />
            <mtLanguage source='en_us' target='fr_fr' />
            <mtLanguage source='en_us' target='de_de' />
            <mtLanguage source='en_us' target='hi_in' />
            <mtLanguage source='en_us' target='it_it' />
            <mtLanguage source='en_us' target='ja_jp' />
            <mtLanguage source='en_us' target='ko_kr' />
            <mtLanguage source='en_us' target='pt_br' />
            <mtLanguage source='en_us' target='ru_ru' />
            <mtLanguage source='en_us' target='es_es' />
            <mtLanguage source='en_us' target='ur_pk' />
            <mtLanguage source='fr_fr' target='en_us' />
            <mtLanguage source='de_de' target='en_us' />
            <mtLanguage source='hi_in' target='en_us' />
            <mtLanguage source='it_it' target='en_us' />
            <mtLanguage source='ja_jp' target='en_us' />
            <mtLanguage source='ko_kr' target='en_us' />
            <mtLanguage source='pt_br' target='en_us' />
            <mtLanguage source='ru_ru' target='en_us' />
            <mtLanguage source='es_es' target='en_us' />
            <mtLanguage source='ur_pk' target='en_us' />
        </application>
    </applications>

    现在,如果我想要访问属性为chat的application节点下的所有mtLanguage子节点,XPath应该这样写:

    //applications/application[@name='chat']/mtLanguage

    而具体操作的Java语句是:

    Document doc= DocumentHelper.parseText(responseXML);// 这个responseXML就是上面的XML样例
    List<?> elms=doc.selectNodes("//applications/application[@name='chat']/mtLanguage");
    System.out.println("There are "+elms.size()+" language pairs available in text translation");
                    
    for(Object obj:elms){
         Element elm=(Element)obj;                    
         System.out.println("From "+elm.attributeValue("source")+" to "+elm.attributeValue("target"));
    }

    执行上面语句输出如下:

    There are 22 language pairs available in text translation
    From ar_ar to en_us
    From zh_cn to en_us
    From zh_tw to en_us
    From en_us to ar_ar
    From en_us to zh_cn
    From en_us to zh_tw
    From en_us to fr_fr
    From en_us to de_de
    From en_us to it_it
    From en_us to ja_jp
    From en_us to ko_kr
    From en_us to pt_br
    From en_us to ru_ru
    From en_us to es_es
    From fr_fr to en_us
    From de_de to en_us
    From it_it to en_us
    From ja_jp to en_us
    From ko_kr to en_us
    From pt_br to en_us
    From ru_ru to en_us
    From es_es to en_us

    2.访问特定节点

    XML样本:

    <rep sts="OK" a="trep" tl="zh-CN">
        <docs>
            <d dt="ndoc" did="d20160223213120480009045125076363146" lang="en-US"
                ctime="2016-02-23T21:31:20" mtime="2016-02-23T21:31:20" orig="1"
                mime="text/x-mt-xml" wc="2">
                <p pid="1" wc="2">
                    <s sid="1">
                        <t tid="1" tt="orig" wc="2">Good evening</t>
                    </s>
                </p>
            </d>
            <d dt="ndoc" did="d20160223213120480009045125076363146" lang="zh-CN"
                ctime="2016-02-23T21:31:20" mtime="2016-02-23T21:31:20" orig="0"
                mime="text/x-mt-xml" sc="100.00" wc="1">
                <p pid="1" wc="1">
                    <s sid="1">
                        <t tid="1" tt="mt" src="mt" sc="100.00" wc="1">晚上好</t>
                    </s>
                </p>
            </d>
        </docs>
    </rep>

    如果我想得到上文中“晚上好”这段文字,XPath应该这样写

    //rep/docs/d[last()]/p/s/t

    对应的Java代码是:

    Document doc= DocumentHelper.parseText(responseXML);
    Element elm = (Element) doc.selectSingleNode("//rep/docs/d[last()]/p/s/t");
    targetTxt=elm.getText();

    3.取属性

    XML样本:

    <rep sts="OK" a="trep" tl="zh-CN">
        <docs>1</docs>
    </rep>

    要取根节点rep的sts属性,XPath可以这样写:

    //rep/@sts

    而对应的Java语句是:

    System.out.println("XML="+responseXML);
    Document doc= DocumentHelper.parseText(responseXML);
    Attribute attr = (Attribute) doc.selectSingleNode("//rep/@sts");      
                
    return attr.getText();
  • 相关阅读:
    玩转TypeScript(2) --简单TypeScript类型
    玩转TypeScript(1) --定义简单的类
    在WisOne平台上学习TypeScript
    为juggle添加了一个js扩展
    和大家分享一个abelkhan的demo
    全服排行榜算法思路
    abelkhan中的rpc框架
    abelkhan编译文档
    abelkhan服务器框架
    一起学习c++11——c++11中的新增的容器
  • 原文地址:https://www.cnblogs.com/heyang78/p/5212053.html
Copyright © 2020-2023  润新知