October 21, 2016
Hot Topics:

Does StAX Belong in Your XML Toolbox?

  • August 20, 2004
  • By Jeff Ryan
  • Send Email »
  • More Articles »

Transform Arbitrary Data Structures to XML

Sometimes, it is useful to view arbitrary data structures as XML. In two of my previous articles, "Transforming Flat Files To XML with SAX and XSLT" and "Converting JDBC Result Sets to XML," I provided detailed examples of this use case.

Both SAX and StAX have the ability to view arbitrary structures as a stream of XML events. With SAX, a class that implements the XMLReader interface is created. This class parses the non XML structure and publishes events to handler classes representing elements, attributes, and data. With StAX, a class is written to parse the non-XML structure and manufacture events using the XMLEventFactory. Of course, DOM could also be used for this task, although it might not be as efficient.

Data Binding

Data binding is another type of transformation where the XML is transformed to and from objects. SAX can be used to unmarshal an XML document to objects. However, because there isn't a writing side of the API, another approach is needed to marshal an XML documents from objects. Because StAX has both a reading and writing side of the API, it is well suited to this purpose. In addition, a "pull" architecture for data binding based upon StAX can simplify code, reduce memory overhead, and improve performance.

Domain Model

Rather than transform XML into objects, sometimes it is useful to retain the incoming XML document and use it as your domain model. For example, if you acquired the data from an XML-based service, and are going to transform it into XHTML, what is the sense in binding it to objects? For this use case, DOM is the only choice. It is the only JAXP API providing true CRUD (Create, Read, Update, Delete) and random access ability to an XML document.

XML Pipelines

Some complex XML processing problems can be decomposed into a series of XML to XML transformations. It would be inefficient for each step in the pipeline to re-parse the XML. Pipelines can be designed with an API to communicate between steps. StAX is the only JAXP API designed specifically to optimize this use case. SAX is often used in pipelines; however, it does not provide a writing side of the API.

Use Case Summary

The following table summarizes which JAXP API is suited to the use cases discussed.

XML to XML Transformation Decent choice for relatively simple structures N/A Decent choice for moderately complex structures Best overall choice
Transform arbitrary data structures to XML Good all-purpose choice Good all-purpose choice N/A N/A
Data Binding Good all-purpose choice Works only for unmarshaling XML May be used in conjunction with other API for buffering extremely complex structures N/A
Domain Model N/A N/A Best overall choice N/A
XML Pipelines Best overall choice Works for reading, but not writing steps in pipeline N/A N/A


StAX is the newest API in the JAXP family. We've studied it over the last three articles and established some initial guidelines for its use. But, please keep in mind that experience will be the best teacher. As the API is used on more and more real-life projects, there will be additional learning to share.


We studied the place of StAX alongside its sister API in the JAXP family. It is well suited to many common XML processing use cases. It just might become the favorite tool in your XML processing toolbox.

In the last three articles, we've laid the groundwork for you to begin using StAX effectively. Are you looking to leverage StAX for your XML processing needs? The rest is up to you!

About the Author

Jeff Ryan is an enterprise architect for Hartford Financial Services Group Inc. 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.


  1. Java Community Process. http://jcp.org, Streaming API for XML JSR-173. Specification Version 1.0, October 2003, p. 18
  2. Java Community Process. http://jcp.org, Streaming API for XML JSR-173. Specification Version 1.0, October 2003, p. 22

# # #

Page 2 of 2

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