background image

Creating an Event Reader

<< Creating an Event Iterator | Running cursor2event Sample >>
<< Creating an Event Iterator | Running cursor2event Sample >>
EVENT
S
AMPLE
­ E
VENT
P
ARSE
.
JAVA
107
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";
case XMLEvent.END_ELEMENT:
return "END_ELEMENT";
case XMLEvent.PROCESSING_INSTRUCTION:
return "PROCESSING_INSTRUCTION";
case XMLEvent.CHARACTERS:
return "CHARACTERS";
case XMLEvent.COMMENT:
return "COMMENT";
case XMLEvent.START_DOCUMENT: