获取节点内部节点的值

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

Get the value of nodes inside a node

问题

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

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

我的XML文件:

<Order>
  <OrderID>
    <OrderConfirmationDate>
      <Date>
       <Year>2000</Year>
       <Month>10</Month>
       <Day>06</Day>
      </Date>
    </OrderConfirmationDate>
  <Adress>
    <Name>Papel do Porto</Name>
      <Address1>Rua da Alegria</Address1>
      <Address2>n&#186;1</Address2>
      <City>Porto</City>
      <PostalCode>4000-032</PostalCode>
      <Country ISOCountryCode="PT">Portugal</Country>
    </NameAddress>

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

dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            
            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:

&lt;Order&gt;
  &lt;OrderID&gt;
    &lt;OrderConfirmationDate&gt;
      &lt;Date&gt;
       &lt;Year&gt;2000&lt;/Year&gt;
       &lt;Month&gt;10&lt;/Month&gt;
       &lt;Day&gt;06&lt;/Day&gt;
      &lt;/Date&gt;
    &lt;/OrderConfirmationDate&gt;
  &lt;Adress&gt;
    &lt;Name&gt;Papel do Porto&lt;/Name&gt;
      &lt;Address1&gt;Rua da Alegria&lt;/Address1&gt;
      &lt;Address2&gt;n&#186;1&lt;/Address2&gt;
      &lt;City&gt;Porto&lt;/City&gt;
      &lt;PostalCode&gt;4000-032&lt;/PostalCode&gt;
      &lt;Country ISOCountryCode=&quot;PT&quot;&gt;Portugal&lt;/Country&gt;
    &lt;/NameAddress&gt;

This is the code I've understood until now

dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            
            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

要选择特定节点,可以使用[XPath](https://www.w3schools.com/xml/xpath_syntax.asp)表达式。

XPath xpath = XPathFactory.newDefaultInstance().newXPath();
XPathExpression expr = xpath.compile("//OrderConfirmationDate/Date");
Element orderConfirmationDateElement = (Element) expr.evaluate(doc, XPathConstants.NODE);
        

然后遍历节点的子节点:

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

输出:

Year: 2000
Month: 10
Day: 06
英文:

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

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

Then to go through the node's children:

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

Output:

Year: 2000
Month: 10
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:

确定