首先,我必须说,我发现 Xpath
是一个非常好的解析器,而且我认为与其他解析器相比它非常强大。
给定以下代码:
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("input.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
如果我想找到 Round 1 & Door 1 的 first
节点,这里:
<Game>
<Round>
<roundNumber>1</roundNumber>
<Door>
<doorName>abd11</doorName>
<Value>
<xVal1>0</xVal1>
<xVal2>25</xVal2>
<pVal>0.31</pVal>
</Value>
<Value>
<xVal1>25</xVal1>
<xVal2>50</xVal2>
<pVal>0.04</pVal>
</Value>
<Value>
<xVal1>50</xVal1>
<xVal2>75</xVal2>
<pVal>0.19</pVal>
</Value>
<Value>
<xVal1>75</xVal1>
<xVal2>100</xVal2>
<pVal>0.46</pVal>
</Value>
</Door>
<Door>
<doorName>vvv1133</doorName>
<Value>
<xVal1>60</xVal1>
<xVal2>62</xVal2>
<pVal>1.0</pVal>
</Value>
</Door>
</Round>
<Round>
<roundNumber>2</roundNumber>
<Door>
<doorName>eee</doorName>
<Value>
<xVal1>0</xVal1>
<xVal2>-25</xVal2>
<pVal>0.31</pVal>
</Value>
<Value>
<xVal1>-25</xVal1>
<xVal2>-50</xVal2>
<pVal>0.04</pVal>
</Value>
<Value>
<xVal1>-50</xVal1>
<xVal2>-75</xVal2>
<pVal>0.19</pVal>
</Value>
<Value>
<xVal1>-75</xVal1>
<xVal2>-100</xVal2>
<pVal>0.46</pVal>
</Value>
</Door>
<Door>
<doorName>cc</doorName>
<Value>
<xVal1>-60</xVal1>
<xVal2>-62</xVal2>
<pVal>0.3</pVal>
</Value>
<Value>
<xVal1>-70</xVal1>
<xVal2>-78</xVal2>
<pVal>0.7</pVal>
</Value>
</Door>
</Round>
</Game>
我会这样做:
XPathExpression expr = xpath.compile("//Round[1]/Door[1]/Value[1]/*/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
如果我想要 Round 1 & Door 1 的 second
节点,那么:
XPathExpression expr = xpath.compile("//Round[1]/Door[1]/Value[2]/*/text()");
但是我如何使用循环执行此操作,因为我不知道我有多少 Value-nodes
,这意味着我如何使用循环执行此操作,每次迭代我检索 3 (我的意思是 xVal1
、 xVal2
和 pVal
值)一个值节点的更多值!?
要求这样做的原因是:
我不知道我有多少
Round
-s我不知道我有多少
Value
-s我不想每次都声明一个新的
XPathExpression
谢谢。
最佳答案
选项 1 - 遍历文档中的所有 Value 元素。只需一次评估,但很难知道该值属于哪个 Round 或 Door 元素。
NodeList result = (NodeList) xpath.evaluate("//Round/Door/Value/*/text()", doc, XPathConstants.NODESET);
选项 2 - 分别迭代每个 Round、Door 和 Value 元素。需要更多的评估,但上下文很容易知道。如果需要索引,很容易在循环中添加一个计数器。
// Get all rounds and iterate over them
NodeList rounds = (NodeList) xpath.evaluate("//Round", doc, XPathConstants.NODESET);
for (Node round : rounds) {
// Get all doors and iterate over them
NodeList doors = (NodeList) xpath.evaluate("Door", round, XPathConstants.NODESET);
for (Node door : doors) {
// Get all values and iterate over them
NodeList values = (NodeList) xpath.evaluate("Value/*/text()", door, XPathConstants.NODESET);
for (Node value : values) {
// Do something
}
}
}
选项 3 - 根据您的要求对上述方法进行一些组合
请注意,我删除了表达式编译步骤以缩短示例。应该重新添加它以提高性能。
关于java - 如何使用 Xpath 在 XML 树的节点后检索节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10463934/