background image

Creating an Input Factory

<< Creating the Allocator Method | Returning the Output >>
<< Creating the Allocator Method | Returning the Output >>

Creating an Input Factory

Building and Running the Cursor-to-Event Example Using Ant
To compile and run the Cursor-to-Event example using Ant, in a terminal window, go to the
tut-install/javaeetutorial5/examples/stax/cursor2event/ directory and type the
following:
ant run-cursor2event
Event Example
Located in the tut-install/javaeetutorial5/examples/stax/event/ directory,
EventParse.java
demonstrates how to use the StAX event API to read an XML document.
Creating an Input Factory
The first step is to create a new instance of XMLInputFactory:
XMLInputFactory factory = XMLInputFactory.newInstance();
System.out.println(
"FACTORY: " + factory);
Creating an Event Reader
The next step is to create an instance of XMLEventReader:
XMLEventReader r = factory.createXMLEventReader(filename,
new FileInputStream(filename));
Creating an Event Iterator
The third step is to create an event iterator:
XMLEventReader r = factory.createXMLEventReader(filename,
new FileInputStream(filename));
while(r.hasNext()) {
XMLEvent e = r.nextEvent();
System.out.println(e.toString());
}
Getting the Event Stream
The final step is to get the underlying event stream:
public final static String getEventTypeString(int eventType) {
switch (eventType) {
case XMLEvent.START_ELEMENT:
return
"START_ELEMENT";
Example Code
Chapter 18 · Streaming API for XML
573