I am using iterate over all nodes in xml document:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File(path)); //path is path to xml document
NodeList nodeList = document.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
// do something with the current element
System.out.println(node.getNodeName() + ": " + node.getTextContent());
}
}
Source: Java: Most efficient method to iterate over all elements in a org.w3c.dom.Document?
And I want print text of each node.
When I call node.getTextContent()
it return me string of all childs in this node.
Example:
<chanel>
<car>opel</car>
<color>orange</color>
</chanel>
Output:
chanel: opel
orange
car: opel
color: orange
But.. item chanel does't have a text opel and orange? How to print only text in actual node?
Like this:
chanel: null car: opel color: orange
Thank you for any help!