I am really confused with the XML DOM Tree structure.
For example I have this piece of XML
<?xml version="1.0" encoding="UTF-8"?>
<Container>
<Group>
</Group>
<Group2>
</Group2>
</Container>
Shouldn’t the Container node consists of only 2 children? Group and 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?
}
Because in between there is are nodes of type TEXT. They are implicit nodes.
Your nodes Group and Group2 are of ELEMENT type. Mostly, following XML will give you count 2,