October 26, 2016
Hot Topics:

Delving Deeper Into StAX

  • June 22, 2004
  • By Jeff Ryan
  • Send Email »
  • More Articles »

Event Iterator API Overview

The event iterator API also has reading and writing sides of the API. As with the cursor API, instances of readers are obtained from the input and output factories. However, there is an additional factory, XMLEventFactory, used to manufacture events.

On the reading side, an iterator API exposes a hasNext() method for reading through the document. The nextEvent() method is used to get a handle on an event. There are various subclasses of events with their own accessor and interrogator methods.

Here is a sample code snippet for reference:


XMLEventReader reader =
   factory.createXMLEventReader(new FileReader("test.xml"'));

while (reader.hasNext())
   XMLEvent event = reader.nextEvent();

   switch (event.getEventType())
      case XMLEvent.START_ELEMENT :
         StartElement se = event.asStartElement();

On the writing side of the API, the XMLEventFactory is used to manufacture events that can be added to the output stream by the XMLEventWriter. Once again, a code snippet is provided for reference:

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter writer =
   outputFactory.createXMLEventWriter(new FileWriter("test.xml"));
XMLEventFactory eventFactory = XMLEventFactory.newInstance();

StartDocument sd = eventFactory.createStartDocument();
StartElement se  = eventFactory.createStartElement("","","Name");
Characters c     = eventFactory.createCharacters("Jeff");
EndElement ee    = eventFactory.createEndElement("","","Name");
EndDocument ed   = eventFactory.createEndDocument();



Let's build a full example to showcase some of the capabilities of the event iterator API. We'll begin with imports from the new stream packages:

package com.developer.stax;

import java.io.*;
import java.util.*;
import javax.xml.stream.*;
import javax.xml.stream.events.*;

public class SimpleXmlEventReader

We'll create an instance of the XMLInputFactory, and ask it for an XMLEventReader. So far, this isn't much different than the cursor API other than the type of reader created.

public static void main(String[] args)

   throws FileNotFoundException, XMLStreamException
   String filename = args[0];

   XMLInputFactory factory = XMLInputFactory.newInstance();

   XMLEventReader reader =
      factory.createXMLEventReader(new FileReader(filename));

Now we start to see some of the differences in the event iterator API.The cursor API would use next() to position the cursor at the next element, attribute, or data. With the event iterator API, we use nextEvent() to get a handle to the next event. A very handy feature is the peek() method that is used to determine what the next sequential event is.

while (reader.hasNext())
   XMLEvent event = reader.nextEvent();
   XMLEvent nextEvent = reader.peek();

Page 2 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel