I want to print the child elements of the root node. This is my XML file.
Why the three text nodes are coming over here ?
They're the whitespace between the child elements. If you only want the child elements, you should just ignore nodes of other types:
for (int i = 0;i < nList.getLength(); i++) {
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("node name: " + node.getNodeName());
}
}
Or you could change your document to not have that whitespace.
Or you could use a different XML API which allows you to easily ask for just elements. (The DOM API is a pain in various ways.)
If you only want to ignore element content whitespace, you can use Text.isElementContentWhitespace.