I've tried your code with your xml, and it prints out the whole text content for me, very strange. Anyway, the Node#getTextContext
method returns the text content of the current node and its descendants.
I suggest you to use node.getFirstChild().getNodeValue()
, which prints out the text content for your node and not its descendants. An other way is iterating over the children of the Suburbs node.
You should also take a look here.
This is my main which prints out the same text for two times, using both getFirstChild().getNodeValue()
and getChildNodes().item(i).getNodeValue()
:
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("dom.xml"));
NodeList nodeList = doc.getElementsByTagName("Suburb");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasChildNodes()) {
System.out.println("<tr><td>Suburb</td>" + "<td>"+node.getFirstChild().getNodeValue()+"</td></tr>");
NodeList textNodeList = node.getChildNodes();
StringBuilder textBuilder = new StringBuilder();
for (int j = 0; j < textNodeList.getLength(); j++) {
Node textNode = textNodeList.item(j);
if (textNode.getNodeType() == Node.TEXT_NODE) {
textBuilder.append(textNode.getNodeValue());
}
}
System.out.println("<tr><td>Suburb</td>" + "<td>" + textBuilder.toString() + "</td></tr>");
}
}
}
This is my output with your xml:
<tr><td>Suburb</td><td>Bondi Junction</td></tr>
<tr><td>Suburb</td><td>Bondi Junction</td></tr>