英文:
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º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:
<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º1</Address2>
<City>Porto</City>
<PostalCode>4000-032</PostalCode>
<Country ISOCountryCode="PT">Portugal</Country>
</NameAddress>
This is the code I've understood until now
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + 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 "node.getNodeName().
(in this case I mean to get the <OrderConfirmationDate>
out of <OrderID>
)
Also, I've read that tags like <Year>
and <Month>
are Ids of the node <Date>
.
答案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("//OrderConfirmationDate/Date");
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 < dateChildren.getLength(); i++) {
if (dateChildren.item(i).getNodeType() == Node.ELEMENT_NODE) {
System.out.println(dateChildren.item(i).getNodeName() + ": " + dateChildren.item(i).getTextContent());
}
}
Output:
Year: 2000
Month: 10
Day: 06
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论