[英]Correct XPathExpression for retrieving Nodes in Java?
I have an XML document that has multiple hpp:HourlyHistoricalPrice
elements as in the following way: 我有一个具有多个
hpp:HourlyHistoricalPrice
元素的XML文档,如下所示:
<?xml version="1.0"> <hhp:HourlyHistoricalPrices xmlns:hhp="urn:or-HourlyHistoricalPrices"> <hhp:HourlyHistoricalPrice xmlns:hhp="urn:or-HourlyHistoricalPrice"> <hhp:indexId>1025127</hhp:indexId> <hhp:resetDate>20161231T000000</hhp:resetDate> <hhp:refSource>AIBO</hhp:refSource> <hhp:indexLocation/> <hhp:price1>50,870000</hhp:price1> ... <hhp:price48>43,910000</hhp:price48> </hhp:HourlyHistoricalPrice> <hhp:HourlyHistoricalPrice xmlns:hhp="urn:or-HourlyHistoricalPrice"> <hhp:indexId>1025127</hhp:indexId> <hhp:resetDate>20160101T000000</hhp:resetDate> <hhp:refSource>AIBO</hhp:refSource> <hhp:indexLocation/> <hhp:price1>51,870000</hhp:price1> ... <hhp:price48>49,910000</hhp:price48> </hhp:HourlyHistoricalPrice> <hhp:HourlyHistoricalPrice xmlns:hhp="urn:or-HourlyHistoricalPrice"> <hhp:indexId>1025127</hhp:indexId> <hhp:resetDate>20163112T000000</hhp:resetDate> <hhp:refSource>APX</hhp:refSource> <hhp:indexLocation/> <hhp:price1>63,870000</hhp:price1> ... <hhp:price48>29,910000</hhp:price48> </hhp:HourlyHistoricalPrice> </hhp:HourlyHistoricalPrices>
I want to retrieve only the hhp:HourlyHistoricalPrice
nodes that have a particular value for hhp:refSource
, eg AIBO
. 我想只检索
hhp:HourlyHistoricalPrice
具有特定值的节点hhp:refSource
,如AIBO
。
I was trying the below XPathExpression but this retrieves nothing. 我正在尝试下面的XPathExpression,但这什么也没检索到。
XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); String strExprssion = "/hhp:HourlyHistoricalPrices/hhp:HourlyHistoricalPrice[hhp:refSource='AIBO']"; XPathExpression expression = xpath.compile(strExprssion); NodeList nodes = (NodeList) expression.evaluate(originalXmlDoc, XPathConstants.NODESET); System.out.println(nodes.getLength());
I would be grateful if somebody could provide advise on the correct expression to use. 如果有人可以就正确的表达方式提供建议,我将不胜感激。
Thanks a lot. 非常感谢。
You need to expand the prefix into the xml namespace it represents: 您需要将前缀扩展到它表示的xml名称空间中:
String strExprssion = "//urn:or-HourlyHistoricalPrice:HourlyHistoricalPrice[urn:or-HourlyHistoricalPrice:refSource='AIBO']";
So for me, this test class 所以对我来说,这个测试班
public class XPathCheck {
public static void main(String[] args) throws FileNotFoundException, IOException, XPathExpressionException {
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
try (InputStream file = new FileInputStream(Paths.get("src", "inputFile.xml").toFile())) {
String strExprssion = "//urn:or-HourlyHistoricalPrice:HourlyHistoricalPrice[urn:or-HourlyHistoricalPrice:refSource='AIBO']";
XPathExpression expression = xpath.compile(strExprssion);
NodeList nodes = (NodeList) expression.evaluate(new InputSource(file), XPathConstants.NODESET);
System.out.println(nodes.getLength());
}
}
}
outputs "2". 输出“ 2”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.