爪哇国新游记之三十四----Dom4j的XPath操作-阿里云开发者社区

开发者社区> 桃子红了呐> 正文

爪哇国新游记之三十四----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();








,如需转载请自行联系原作者



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
9206 0
图文解析PHP对象的赋值操作是“传值”还是“传址”
通过本文,我们一起分享了在PHP中对象的赋值操作,传递的是对象地址,而不是复制对象。PHP中可以使用clone操作符来复制对象。
1359 0
AliOS Things技术底层加持cYoC物联网操作系统 获国际TUeV功能安全认证
近日,以阿里云物联网操作系统AliOS Things技术底层加持、平头哥半导体有限公司自主研发的YoC软件平台斩获了国际公认第三方认证机构SGS-TUeV SAAR颁发的SIL3功能安全证书。这标志着平头哥YoC软件平台,具备了与国际规范接轨的能力,也进一步体现了阿里云自产自研的AliOS Things系统底层的技术含量与端侧实力。
952 0
4269
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载