// Logic
XMLInputFactory factory = XMLInputFactory.newInstance();
Reader fileReader = new FileReader("1.xml");
XMLEventReader reader = factory.createXMLEventReader(fileReader);
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
StartElement element = (StartElement) event;
System.out.println("Start Element: " + element.getName());
Iterator iterator = element.getAttributes();
while (iterator.hasNext()) {
Attribute attribute = (Attribute) iterator.next();
QName name = attribute.getName();
String value = attribute.getValue();
System.out.println("Attribute name/value: " + name + "/" + value);
}
}
if (event.isEndElement()) {
EndElement element = (EndElement) event;
System.out.println("End element:" + element.getName());
}
if (event.isCharacters()) {
Characters characters = (Characters) event;
System.out.println("Text: " + characters.getData());
}
}
這裏這個示例XML文件我需要迭代XML缺少結束標籤?
<Bank>
<Account type="saving">
<Id>1001</Id>
<Name>Jack Robinson
<Number>7898998
<Amt>10000</Amt>
</Account>
</Bank>
Eggen我接受了你的答案,但是你能描述如何寫java 1.4嗎?可能嗎? – Suresh
@Suresh即將到來的Java 8是如此的稚嫩;我希望你能夠在2014年切換。 –