background image

Creating an Event Iterator

<< cursor2event Sample | Creating an Event Reader >>
<< cursor2event Sample | Creating an Event Reader >>
106
S
TREAMING
API
FOR
XML
Creating an Event Iterator
The next step is to create an event iterator:
int eventType = xmlr.getEventType();
while(xmlr.hasNext()){
eventType = xmlr.next();
//Get all "Book" elements as XMLEvent object
if(eventType == XMLStreamConstants.START_ELEMENT &&
xmlr.getLocalName().equals("Book")){
//get immutable XMLEvent
StartElement event = getXMLEvent(xmlr).asStartElement();
System.out.println("EVENT: " + event.toString());
}
}
Creating the Allocator Method
The final step is to create the
XMLEventAllocator
method:
private static XMLEvent getXMLEvent(XMLStreamReader reader)
throws XMLStreamException{
return allocator.allocate(reader);
}
Running the Sample
When you run the
CursorApproachEventObject
sample, the class is compiled,
and the XML stream is parsed and returned to
STDOUT
. Note how the
Book
events
are returned as strings.
event Sample ­ EventParse.java
Located in the
<javaee.tutorial.home>/examples/stax/event
directory,
EventParse.java
demonstrates how to use the StAX event API to read an XML
document.