I have an xml like this :
<root>
<countries>
<country id="98" nom="Espagne"/>
<country id="76" nom="France"/>
</countries>
</root>
I can read inside root tag with this :
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(XmlFile);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
Node NodeCountries = doc.getElementsByTagName("countries").item(0);
System.out.println(nodeToString(NodeCountries));
private static String nodeToString(Node node) throws Exception{
StringWriter sw = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
}
But I can not get all content inside countries tag like this :
<country id="98" nom="Espagne"/>
<country id="76" nom="France"/>