【发布时间】:2017-01-28 18:10:04
【问题描述】:
我对 XPathExpression 的工作还是很陌生,我正在尝试构建一个表达式来将属性值检索到我的自定义 java 对象中,即 Message。下面是一个虚拟 XML,其中有许多 message 节点。
我的 Java 代码应该读取这个 XML,并相应地为 messages 创建一个 Java 对象列表。
这是我尝试构建 XpathExpression 的条件。
- 在此 Xpath jobReport/records/record/messages 中检查 title 是否具有值 TestCompany。如果有任何其他公司消息应跳过
我正在使用下面的 Xpath 表达式,但由于命名空间数据,它无法返回数据。现在我需要使用表达式跳过这些名称空间并获取 Title 值。
- jobReport/records/record/messages/*[local-name()='aa:title']
- jobReport/records/record/messages/*[local-name()='aa:title'/text()='TestCompany']
- //*[local-name()='aa:title'/text()='TestCompany']
j
<?xml version="1.0" encoding="UTF-8"?>
<jobReport>
<jobID>123515412512221</jobID>
<rundate>2016-09-16</rundate>
<startTime>13:09:49</startTime>
<endTime>13:10:39</endTime>
<containsErrors>false</containsErrors>
<records>
<record>
<recordid>20160920001</recordid>
<primaryfilename>hello.pdf</primaryfilename>
<result>
<status>OK</status>
<errorcode />
</result>
<messages>
<aa:title xmlns="http://www.somedomain.com/example/test/xml/"
xmlns:aa="DummyProject/2016-04">TestCompany</aa:title>
<aa:messageid xmlns="http://www.somedomain.com/example/test/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">30.02.02.15.01</aa:messageid>
<aa:messagefrom xmlns="http://www.somedomain.com/example/test/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">FI</aa:messagefrom>
<aa:messageto
xmlns="http://www.op.fi/hallintapalvelut/dokumenttirajapinta/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">keijo</aa:messageto>
<aa:mailid
xmlns="http://www.op.fi/hallintapalvelut/dokumenttirajapinta/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">agreement</aa:mailid>
<aa:phonenumber
xmlns="http://www.op.fi/hallintapalvelut/dokumenttirajapinta/xml/"
xmlns:aa="OP360CustomerArchive/2016-04">xml-tuote</aa:phonenumber>
</messages>
</record>
</records>
</jobReport>
【问题讨论】:
标签: java xml xpath xml-namespaces