JAXP StAX offers two different set of APIs: a cursor API and an iterator API. We have seen StAX Stream Reader Example which is based on cursor API.
In this article, we will see an example of iterator API. Each chunk of the XML is represented by an event. We will see how an employee XML can be read using the StAX event reader.
The parser creates different event types as it moves forward reading the source XML document.
Few of the important event types are:
- Start document
- Start element
- Comments
- Characters
- End Element
- End document
Employee:
package com.javarticles.jaxp; public class Employee { private String name; private Integer age; private Integer id; Employee(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String toString() { return "Employee(id:"+ id + ", name:" + name + ", age:" + age + ")"; } }
Here is the Employee XML.
sample.xml:
<?xml version="1.0"?> <employees> <employee id="1"> <name>Joe</name> <age>34</age> </employee> <employee id="2"> <name>Sam</name> <age>24</age> </employee> <employee id="3"> <name>John</name> <age>44</age> </employee> </employees>
In order to create the XML event reader, we need to first obtain an instance of the factory.
XMLInputFactory factory = XMLInputFactory.newInstance();
We can further configure the factory to resolve resources, turn on/off validation against the dtd, namespace processing etc.
Next we create the XMLEventReader
.
XMLEventReader reader = factory.createXMLEventReader("sample.xml", new FileInputStream("src/com/javarticles/jaxp/sample.xml"));
Once the event reader is created, we retrieve the XMLEvents using the iterator API.
while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); } ...
The event retrieved is of base type XMLEvent
and the actual sub-class it represents depends the event type, for example, if the parsing of an element has started, then the event represents StartElement
.
if (event.isStartElement()) { StartElement element = (StartElement) event; ... }
The attributes of an element can be accessed from the StartElement
using getAttributes()
. The returned object again is an instance of Iterator
which is why Attribute
events are called as secondary events.
StartElement element = (StartElement) event; ... Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); ... }
StAXEventReaderExample:
package com.javarticles.jaxp; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Iterator; import javax.xml.namespace.QName; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; public class StAXEventReaderExample { public static void main(String[] args) throws XMLStreamException, FileNotFoundException { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader reader = factory.createXMLEventReader("sample.xml", new FileInputStream("src/com/javarticles/jaxp/sample.xml")); 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() + "]"); } } } }
Output:
Start Element: employees Text:[ ] Start Element: employee Attribute name/value: id/1 Text:[ ] Start Element: name Text:[Joe] End element:name Text:[ ] Start Element: age Text:[34] End element:age Text:[ ] End element:employee Text:[ ] Start Element: employee Attribute name/value: id/2 Text:[ ] Start Element: name Text:[Sam] End element:name Text:[ ] Start Element: age Text:[24] End element:age Text:[ ] End element:employee Text:[ ] Start Element: employee Attribute name/value: id/3 Text:[ ] Start Element: name Text:[John] End element:name Text:[ ] Start Element: age Text:[44] End element:age Text:[ ] End element:employee Text:[ ] End element:employees
Download the source code
This was an example of JAXP StAX Event Reader.