Does StAX Stack Up?
There is a new XML API in town called StAX (Streaming API for XML). With SAX (Simple API for XML), DOM (Document Object Model), and TrAX (Transformation API for XML), do we really need another API in the JAXP (Java API for XML Processing) family for processing XML documents? I would argue, yes.
In this article, we'll discuss how StAX can be more developer friendly than SAX and how it can perform better than DOM and TrAX for certain use cases. We'll discuss where to download the reference implementation, and how to configure the classpath for your first StAX project. Then, we'll develop simple programs to read and write XML documents using StAX. Finally, we'll summarize our first experience with StAX.
How Does StAX Stack Up?
Where does StAX fit in with its sister API for processing XML documents?
Despite its name, SAX isn't all that simple. In fact, it's kind of awkward at first. The developer writes a handler class that receives event callbacks. It is a "push" type of API that requires reading the entire XML document. SAX is strictly an API for reading XML documents. You must learn another API to write XML documents.
DOM is a very powerful API that can be used to create, query, and manipulate XML documents. However, it requires an in-memory representation of the document, which can be costly in terms of performance.
TrAX is an API for transforming source documents into result documents using XSLT (XML Stylesheet Language for Transformations), a declarative, rule-based language. As with DOM, a performance penalty may be paid for its significant power.
How does StAX stack up? StAX is even simpler than SAX. It is a "pull" API that gives parsing control to the developer by exposing an iterator-based API and an underlying stream of events[i]. It has an API to read and write XML documents more efficiently than DOM or TrAX.
StAX won't replace its sister API. It doesn't have the power of DOM and TrAX. It can't expose arbitrary data structures as XML as SAX can. But, it can handle certain XML use cases more efficiently or simply than its predecessors. We'll discuss which API is suited to a given use case in a future article. Let's get grounded in StAX before we make comparisons.
Downloading and Configuring StAX
The StAX API was developed as part of the Java Community Process and is JSR-173. The API Specification can be downloaded from http://jcp.org/en/jsr/detail?id=173. The reference implementation can be downloaded from http://dev2dev.bea.com/technologies/stax/index.jsp.
There is a runtime dependency of the reference implementation on Java 1.4 for its new non-blocking I/O capabilities that can be downloaded at http://java.sun.com.
You will find the following jars in the reference implementation download that you should include in your classpath:
|jsr173_1.0_api.jar||This jar represents the StAX public interface.|
|jsr173_1.0_ri.jar||This jar includes the reference implementation of StAX. |
|xml-apis.jar||This jar includes the implementation of the JAXP API that now includes StAX.|
Page 1 of 2