获取节点内部节点的值

huangapple go评论101阅读模式
英文:

Get the value of nodes inside a node

问题

我一直在尝试找到一个清晰的解决方案来解决我的问题,因为迄今为止我找到的每个解释都让我感到困惑(至少对我来说是这样)。

我已经尝试过DOM阅读器,但我无法将其与这个文件配合使用,因为这是一个更复杂的XML文件。

我的XML文件:

  1. <Order>
  2. <OrderID>
  3. <OrderConfirmationDate>
  4. <Date>
  5. <Year>2000</Year>
  6. <Month>10</Month>
  7. <Day>06</Day>
  8. </Date>
  9. </OrderConfirmationDate>
  10. <Adress>
  11. <Name>Papel do Porto</Name>
  12. <Address1>Rua da Alegria</Address1>
  13. <Address2>n&#186;1</Address2>
  14. <City>Porto</City>
  15. <PostalCode>4000-032</PostalCode>
  16. <Country ISOCountryCode="PT">Portugal</Country>
  17. </NameAddress>

这是我到目前为止理解的代码:

  1. dBuilder = dbFactory.newDocumentBuilder();
  2. Document doc = dBuilder.parse(xmlFile);
  3. doc.getDocumentElement().normalize();
  4. System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

因此,我可以使用getDocumentElement()轻松获取文档的根元素。我可以使用getNodeName()轻松获取根节点的一个节点。

但是,我如何获取node.getNodeName()(在这种情况下,我的意思是从<OrderID>中获取<OrderConfirmationDate>)?
此外,我已经阅读到,像<Year><Month>这样的标签是<Date>节点的标识符。

英文:

I've been trying to find a clear solution for my problem since every explanation I've found until now its confusing (for me at least).

I've tried DOM readers but I can't complement it with this file since its a more complex XML file.

My XML file:

  1. &lt;Order&gt;
  2. &lt;OrderID&gt;
  3. &lt;OrderConfirmationDate&gt;
  4. &lt;Date&gt;
  5. &lt;Year&gt;2000&lt;/Year&gt;
  6. &lt;Month&gt;10&lt;/Month&gt;
  7. &lt;Day&gt;06&lt;/Day&gt;
  8. &lt;/Date&gt;
  9. &lt;/OrderConfirmationDate&gt;
  10. &lt;Adress&gt;
  11. &lt;Name&gt;Papel do Porto&lt;/Name&gt;
  12. &lt;Address1&gt;Rua da Alegria&lt;/Address1&gt;
  13. &lt;Address2&gt;n&#186;1&lt;/Address2&gt;
  14. &lt;City&gt;Porto&lt;/City&gt;
  15. &lt;PostalCode&gt;4000-032&lt;/PostalCode&gt;
  16. &lt;Country ISOCountryCode=&quot;PT&quot;&gt;Portugal&lt;/Country&gt;
  17. &lt;/NameAddress&gt;

This is the code I've understood until now

  1. dBuilder = dbFactory.newDocumentBuilder();
  2. Document doc = dBuilder.parse(xmlFile);
  3. doc.getDocumentElement().normalize();
  4. System.out.println(&quot;Root element :&quot; + doc.getDocumentElement().getNodeName());

So, I can easily get the root of document with getDocumentElement() . And I can easily get a node of the root with getNodeName().

But how do I get the &quot;node.getNodeName(). (in this case I mean to get the &lt;OrderConfirmationDate&gt; out of &lt;OrderID&gt;)
Also, I've read that tags like &lt;Year&gt; and &lt;Month&gt; are Ids of the node &lt;Date&gt;.

答案1

得分: 1

  1. 要选择特定节点,可以使用[XPath](https://www.w3schools.com/xml/xpath_syntax.asp)表达式。
  2. XPath xpath = XPathFactory.newDefaultInstance().newXPath();
  3. XPathExpression expr = xpath.compile("//OrderConfirmationDate/Date");
  4. Element orderConfirmationDateElement = (Element) expr.evaluate(doc, XPathConstants.NODE);

然后遍历节点的子节点:

  1. NodeList dateChildren = orderConfirmationDateElement.getChildNodes();
  2. for(int i = 0; i < dateChildren.getLength(); i++) {
  3. if (dateChildren.item(i).getNodeType() == Node.ELEMENT_NODE) {
  4. System.out.println(dateChildren.item(i).getNodeName() + ": " + dateChildren.item(i).getTextContent());
  5. }
  6. }

输出:

  1. Year: 2000
  2. Month: 10
  3. Day: 06
英文:

To select a particular node, you can use a XPath expression.

  1. XPath xpath = XPathFactory.newDefaultInstance().newXPath();
  2. XPathExpression expr = xpath.compile(&quot;//OrderConfirmationDate/Date&quot;);
  3. Element orderConfirmationDateElement = (Element) expr.evaluate(doc, XPathConstants.NODE);

Then to go through the node's children:

  1. NodeList dateChildren = orderConfirmationDateElement.getChildNodes();
  2. for(int i = 0; i &lt; dateChildren.getLength(); i++) {
  3. if (dateChildren.item(i).getNodeType() == Node.ELEMENT_NODE) {
  4. System.out.println(dateChildren.item(i).getNodeName() + &quot;: &quot; + dateChildren.item(i).getTextContent());
  5. }
  6. }

Output:

  1. Year: 2000
  2. Month: 10
  3. Day: 06

huangapple
  • 本文由 发表于 2020年10月26日 00:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64526165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定