I'm reading a XML file same as below:
<ts>
<tr comment="" label="tr1">
<node order="1" label="" />
</tr>
</ts>
And I expected the below code prints out three e
on screen:
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamReader.START_DOCUMENT) {
continue;
} else if (eventType == XMLStreamReader.END_ELEMENT) {
System.out.println("e");
} else if (eventType == XMLStreamReader.START_ELEMENT) {
System.out.println("s");
}
}
But it doesn't work! Any ideas on how I can resolve the issue?
Note: I think it is related to self-closed-tags, for example: <myTag id="1" />
System.out.println(sr.getLocalName())
inside the if block forEND_ELEMENT
? – Parabola