java 如何使用 Xpath 在 XML 树的节点之后检索节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10463934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:09:05  来源:igfitidea点击:

How to retrieve node after node of XML tree using Xpath?

javaxmlxpathxml-parsing

提问by JAN

First, I must say that I find Xpathas a very nice parser , and I guess pretty powerful when comparing it to other parsers .

首先,我必须说我发现它Xpath是一个非常好的解析器,并且在与其他解析器进行比较时我想它非常强大。

Given the following code :

鉴于以下代码:

  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();
  domFactory.setNamespaceAware(true); 
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("input.xml");
  XPath xpath = XPathFactory.newInstance().newXPath();

If I wanted to find the firstnode of Round 1 & Door 1 , here :

如果我想找到firstRound 1 & Door 1的节点,在这里:

<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>

I'll do this :

我会这样做:

 XPathExpression expr = xpath.compile("//Round[1]/Door[1]/Value[1]/*/text()");      
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;

and if I wanted the secondnode of Round 1 & Door 1 then :

如果我想要second第 1 轮和第 1 门的节点,则:

XPathExpression expr = xpath.compile("//Round[1]/Door[1]/Value[2]/*/text()");  

but how do I do this using a loop , since I don't know how much Value-nodesI have , meaning how can I do this using a loop , where each iteration I retrieve 3 (I mean the xVal1, xVal2and pValvalues ) more values of a Value node !?

但是我如何使用循环来做到这一点,因为我不知道Value-nodes我有多少,这意味着我如何使用循环来做到这一点,每次迭代我检索 3(我的意思是xVal1xVal2pVal值)更多的值的值节点!?

The reasons for asking for this are :

提出这个要求的原因是:

  1. I don't know how much Round-s I have

  2. I don't know how much Value-s I have

  3. I don't want to declare every time a new XPathExpression

  1. 我不知道Round我有多少-s

  2. 我不知道Value我有多少-s

  3. 我不想每次都声明一个新的 XPathExpression

Thanks .

谢谢 。

回答by erikxiv

Option 1- Iterate over all Value elements in the document. Only one evaluation required, but difficult to know which Round or Door element the Value belongs to.

选项 1- 迭代文档中的所有 Value 元素。只需要一个评估,但很难知道该值属于哪个圆形或门元素。

NodeList result = (NodeList) xpath.evaluate("//Round/Door/Value/*/text()", doc, XPathConstants.NODESET);

Option 2- Iterate over each Round, Door and Value elements separately. Requires more evaluations but the context is easily known. If index is required, it is easy to add a counter to the loops.

选项 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
    }
  }
}

Option 3- Do some combination of the above depending on your requirements

选项 3- 根据您的要求对上述进行一些组合

Note that I've removed the expression compilation step to shorten the example. It should be re-added to improve performance.

请注意,我删除了表达式编译步骤以缩短示例。应该重新添加它以提高性能。