Wie kann ich Knoten für Knoten eines XML-Baums mit Xpath abrufen?
Zuerst muss ich sagen, dass ich findeXpath
Als sehr netter Parser, und ich denke ziemlich mächtig, wenn man es mit anderen Parsern vergleicht.
Mit folgendem Code:
<code> DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("input.xml"); XPath xpath = XPathFactory.newInstance().newXPath(); </code>
Wenn ich das finden wolltefirst
Knoten von Runde 1 & Tür 1, hier:
<code><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> </code>
Ich mache das:
<code> XPathExpression expr = xpath.compile("//Round[1]/Door[1]/Value[1]/*/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; </code>
und wenn ich das wolltesecond
Knoten von Runde 1 & Tür 1 dann:
<code>XPathExpression expr = xpath.compile("//Round[1]/Door[1]/Value[2]/*/text()"); </code>
aber wie mache ich das mit einer schleife, da ich nicht weiß wie vielValue-nodes
Ich habe, das heißt, wie kann ich dies mit einer Schleife tun, bei der ich bei jeder Iteration 3 abrufe (ich meine diexVal1
, xVal2
undpVal
Werte) mehr Werte eines Werteknotens!?
Die Gründe dafür sind:
Ich weiß nicht wie vielRound
-s habe ich
Ich weiß nicht wie vielValue
-s habe ich
Ich möchte nicht jedes Mal ein neues deklarierenXPathExpression
Vielen Dank .