I am reading an XML file as shown below:
<ts> <tr comment="" label="tr1"> <node order="1" label="" /> </tr> </ts>
And I expected the code below to display three e :
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 that will not work! Any ideas on how I can solve the problem?
Note. I think this is due to self-closing tags, for example: <myTag id="1" />
source share