Java Dom parser reports wrong number of child nodes
children, dom, java, xml
Solution
There are three child nodes:
- a text node containing a line break
- an element node (tagged user)
- a text node containing a line break
So when processing the child nodes, check for element nodes.
Problem
I have the following xml file: ``` <?xml version="1.0" encoding="UTF-8"?> <users> <user id="0" firstname="John"/> </users> ``` Then I'm trying to parse it with java, but getchildnodes reports wrong number of child nodes. Java code: ``` DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(this.file); document.getDocumentElement().normalize(); Element root = document.getDocumentElement(); NodeList nodes = root.getChildNodes(); System.out.println(nodes.getLength()); ``` Result: 3 Also I'm getting NPEs for accessing the nodes attributes, so I'm guessing something's going horribly wrong.