October 24, 2016
Hot Topics:

Delving Deeper Into StAX

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

We can determine the type of event by using the switch construct over the event type, or by interrogator methods. This is true of both the cursor and event API. However, the event API has asXxx() methods such as asStartElement() to type the proper event object without casting. In the followingcode, StartElement, Character, and EndElement events are parsed. Notice how the nextEvent instance variable obtained via peek() is used to determine whether or not the StartElement has characters.

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

         System.out.print("<" + se.getName());

         Iterator attributes = se.getAttributes();
         while (attributes.hasNext())
            Attribute attr = (Attribute)attributes.next();
            System.out.print(" " + attr.getName() +
                             "=\"" + attr.getValue() + "\"");


         if (nextEvent.isCharacters() )
            Characters c = reader.nextEvent().asCharacters();
            if (!c.isWhiteSpace())

      case XMLEvent.END_ELEMENT>
         EndElement ee = event.asEndElement();
         System.out.print("</" + ee.getName() + ">");

In case you cannot tell by the example, SimpleXmlEventReader simply echoes the input document to the System.out object.


Now, let's build a full example to showcase the event writing API. We begin with imports from the new streaming packages.

package com.developer.stax;

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

public class SimpleXmlEventWriter

Notice how the main method below is expecting two input parameters. The first parameter, args[0], represents the input file to be merged or wrapped in the output file specified in args[1].

public static void main(String[] args)
   throws FileNotFoundException, XMLStreamException, IOException

   String inFile  = args[0];
   String outFile = args[1];

Here we get an instance of an XMLEventWriter from the XMLOutputFactory. We also get an XMLEventFactory for manufacturing events.

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter writer =
   outputFactory.createXMLEventWriter(new FileWriter(outFile));
XMLEventFactory eventFactory = XMLEventFactory.newInstance();

Before writing to the output stream, we need to create the needed events. Because we are going to be wrapping the input document, we'll create the wrapper element and add it to the output stream.

StartDocument sd = eventFactory.createStartDocument();
StartElement se  = eventFactory.createStartElement("","","Wrapper");

The next section of code bears special interest. Here, we get an event reader for the passed input file. Then, we simply add it to the output stream. Merging XML documents has never been easier than this.

// merge file
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader =
    factory.createXMLEventReader(new FileReader(inFile));
reader.next();    // read past processing instruction

Finally, we manufacture the EndElement and EndDocument events, add them to the output stream, and close the writer. The call to SimpleXmlEventReader will echo our wrapper document to System.out.

   EndElement ee = eventFactory.createEndElement("","","Wrapper");
   EndDocument ed = eventFactory.createEndDocument();


   String[] argv = new String[1];
   argv[0] = outFile;


Page 3 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