Вопрос по java, xml – Как получить узел за узлом дерева XML, используя Xpath?

3

Во-первых, я должен сказать, что я нахожуXpathError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

Error: User Rate Limit ExceededfirstError: User Rate Limit Exceeded

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

Error: User Rate Limit ExceededsecondError: User Rate Limit Exceeded

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

Error: User Rate Limit ExceededValue-nodesError: User Rate Limit ExceededxVal1 , xVal2Error: User Rate Limit ExceededpValError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

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

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

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded ron
Error: User Rate Limit Exceeded ron
Error: User Rate Limit Exceeded erikxiv
Error: User Rate Limit Exceededcount()Error: User Rate Limit Exceeded Dimitre Novatchev
Error: User Rate Limit Exceededcount((//Round/Door)[1]/Value)Error: User Rate Limit Exceededcount((//Round/Door)[$k]/Value) Dimitre Novatchev

Ваш Ответ

1   ответ
7

Option 1Error: User Rate Limit Exceeded

//Round/Door/Value/*/text()", doc, XPathConstants.NODESET);

Option 2Error: User Rate Limit Exceeded

// 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 3Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededNodeListError: User Rate Limit ExceededIterableError: User Rate Limit Exceeded
Error: User Rate Limit ExceededNodeListError: User Rate Limit ExceededIterable?

Похожие вопросы