0
我真的很困惑XML DOM樹結構。Java XML節點長度
比如我有這塊XML的
<?xml version="1.0" encoding="UTF-8"?>
<Container>
<Group>
</Group>
<Group2>
</Group2>
</Container>
應該不是容器節點僅包括2名兒童?組和Group2?
File fXmlFile = new File("Test2.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
Node firstNode = doc.getDocumentElement();
if (firstNode.getNodeName().toString().equals("Container")) {
// Process container here
Container container = new Container();
System.out.println(firstNode.getChildNodes().getLength()); // why print out 5?
}
如果我在迭代時省略這些隱式節點,會出現任何問題嗎?我用if條件來檢查getNodeType()== Node.ELEMENT_NODE。 – humansg 2012-07-24 05:25:41
根據你的XML,你不需要那些TEXT節點,所以它不會影響你。您添加的檢查就足夠了。 – devang 2012-07-24 06:40:12