【发布时间】:2016-04-20 12:40:50
【问题描述】:
今天我试图知道一个节点中的一个值,但有时我有一个封闭的节点......例如 XML:
<Info>
<Personne>
<field name="Name" type="String">
<value type="String"> TOTO </value>
</field>>
<field name="CountryCode" type="String">
<value type="String"> Fr </value>
</field>
</Personne>
<Personne>
<field name="Name" type="String">
<value type="String"> TOTO </value>
</field>>
<field name="CountryCode" type="String">
<empty />
</field>
</Personne>
</Info>
要读取我的 XML 文件(在我的 java 代码中),我使用:
expression = "/Personnes/field[@name='Name']/value | /Personnes/field[@name='CountryCode']/value ";
NodeList nodes = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.print(nodes.item(i).getFirstChild().getNodeValue()
}
但我想当 xPath 读取 <empty /> 时,它会通过示例向我显示“EMPTY”而不是任何内容..
非常感谢!
【问题讨论】:
标签: java xml xpath expression