January 27, 2021
Hot Topics:

Does StAX Stack Up?

  • By Jeff Ryan
  • Send Email »
  • More Articles »

Learning to Read and Write

Now that we have the reference implementation of StAX downloaded and ready to use, let's learn how to read and write XML documents with StAX. We'll begin by first writing an XML document, and then we'll echo it back.


SimpleXmlWriter will live up to its name by being a very simple example of how to write an XML document. We begin by importing classes from the new javax.xml.stream package, as well as old standbys from java.io.

package com.developer.stax;

import java.io.FileWriter;
import java.io.IOException;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

We begin by getting an instance of the XMLOutputFactory. Then, we ask the factory for an XMLStreamWriter that wraps a FileWriter object for the passed filename.

public class SimpleXmlWriter
   public static void main(String args[])
      throws IOException, XMLStreamException
      String filename = args[0];

      XMLOutputFactory factory = XMLOutputFactory.newInstance();

      XMLStreamWriter writer =
         factory.createXMLStreamWriter(new FileWriter(filename));

The XMLStreamWriter has methods for writing elements, attributes, and data to an XML document. The stream writing API provides writeStartXxx() and writeEndXxx() methods for the document itself, and for elements; they need to come in pairs. The indenting in the following example helps to show the matching pairs.

         writer.writeCharacters("ABC Pizza");
         writer.writeCharacters("1 Main Street");


The flush() method at the end is needed to write the document and close the FileWriter.

Sample Output

Here is our first XML document created via StAX:

<?xml version="1.0" encoding="utf-8"?>
      <Name>ABC Pizza</Name>
      <Address>1 Main Street</Address>


Now, let's read the document we just created. This time, we import classes from the reading side of the API from javax.xml.stream and some old standby from java.io.

package com.developer.stax;

import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

We begin by getting an instance of an XMLInputFactory. Then, we ask the factory for an XMLStreamReader that wraps a FileReader object for the passed filename.

public class SimpleXmlReader
   public static void main(String[] args)
      throws FileNotFoundException, XMLStreamException
      String filename = args[0];

      XMLInputFactory factory = XMLInputFactory.newInstance();

      XMLStreamReader reader =
         factory.createXMLStreamReader(new FileReader(filename));

The iterator-based API for reading streams is very familiar to Java developers. There is a hasNext() as well as a next() method. In this example, we use hasNext() to iterate through the entire document. Start and End elements are echoed to the output stream via getText(). Data is also output using getText(); however, whitespace is ignored. After the cursor has iterated over the entire stream, we close the XMLStreamReader.

      while (reader.hasNext())
         if (reader.isStartElement() || reader.isEndElement())
         else if (reader.isCharacters() && !reader.isWhiteSpace())

Sample Code

The sample code can be downloaded here.


StAX is a new addition to the JAXP family. It may be easier to use than SAX, and more efficient than DOM and TrAX, although it is less powerful. We developed very simple classes for reading and writing documents through the streaming API. In future articles, we'll delve deeper into StAX. Until then, the rest is up to you!

About the Author

Jeff Ryan is an enterprise architect for Hartford Financial Services. He has twenty years experience designing, developing, and delivering automated solutions to business problems. His current focus is on Java, XML, and Service Oriented Architecture. He may be reached at jeffreyjryan@aol.com.

Other Articles Written by Jeff Ryan

[i] Java Community Process http://jcp.org, Streaming API for XML JSR-173 Specification Version 1.0, October 2003

Page 2 of 2

This article was originally published on May 10, 2004

Enterprise Development Update

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

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