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" />
The code posted in your question produced the three
efor me which is expected. I’m using JDK 1.6 on the Mac.Demo
You may want to try running the following code to see which end element event you are missing:
Output
Below is the output I get.