我真的对 XML DOM 树结构感到困惑。
例如我有这段 XML
<?xml version="1.0" encoding="UTF-8"?>
<Container>
<Group>
</Group>
<Group2>
</Group2>
</Container>
Container 节点不应该只包含 2 个子节点吗?组和组2?
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?
}