StAX « XML « Java Articles

Home
Java Articles
1.Build Deploy
2.Class
3.Core Library
4.Data Types
5.Database JDBC
6.Design
7.Development
8.File Input Output
9.Graphics Desktop
10.J2EE Enterprise
11.J2ME Wireless
12.JVM
13.Language
14.Library Product
15.Network
16.Security
17.SOA Web Services
18.Test
19.Web Development
20.XML
Java Articles » XML » StAX 
Both of these models give little or no control to the user in this parsing process. Once started, whether tree-based or event-based, both parsing approaches consume the whole data stream at once. JSR 173 defines a pull streaming model, StAX (short for "Streaming API for XML"), for processing XML documents. In this model, unlike in SAX, the client can start, proceed, pause, and resume the parsing process. The client has complete control.

Most parsers fall into two broad categories: tree based (e.g., DOM) or event based (e.g., SAX). Although StAX is more closely aligned with the latter, it bridges the gap between the two. In SAX, data is pushed via events to application code handlers. In StAX, the application "pulls" the data from the XML data stream at its convenience. Application code can filter, skip tags, or stop parsing at any time. The application--not the parser--is in control, which enables a more intuitive way to process data.

JSR-173 introduces the Streaming API for XML (StAX) which is a standardized Java based API for pull-parsing XML, developed though the community process. The group is inviting Java developers to read the paper and join the discussion with the Java Performance Community.

A "pull" parsing alternative to SAX's "push" parsing has lurked in the background for some time, but no longer: the recently ratified StAX specification now standardizes a pull parser for Java. StAX provides an alternative processing model where you call methods on the parser at your leisure and move the processing along at your command. The key difference here is that with SAX you don't have control of the application thread and can only accept invocations from the parser. In contrast, with pull parsing you own the application thread and you control when and where you call the XML parser.

The other API style provided by StAX is centered around event objects. Like its cursor-based alternative, it is also a pull-based method of parsing XML; the application pulls each event from the parser by using one of the provided methods, then deals with the event as needed, and so on, until the stream is parsed (or the application decides to stop parsing).

The Streaming API for XML (StAX) completely changes this. Unlike the Simple API for XML (SAX), StAX offers an API for writing XML documents. To be precise, it offers two APIs: a low-level, cursor-based API (XMLStreamWriter), and a higher-level, event-based API (XMLEventWriter). While the cursor-based API is best used in data binding scenarios (for example, creating a document from application data), the event-based API is typically used in pipelining scenarios where a new document is constructed from the data of input documents.

Because the pull-based approach used by StAX leaves the application in control of the parsing process, nothing precludes you from converting the parsed events into application-specific model objects (such as, proprietary messages, or other structural building blocks). However, you might find it more convenient to stay in the realm of events and simply create customized events to represent more complex structures in your XML content. By super-imposing your custom types over their underlying XML data structures, you can simplify the development of your application code while allowing the lower layers to still work with these types as event objects (and, for instance, write them out into an output stream as events).

Since its inception, the Java API for XML Processing (JAXP) provided two methods for processing XML -- the Document Object Model (DOM) method, which uses a standard object model to represent XML documents, and the Simple API for XML (SAX) method, which uses application-supplied event handlers to process XML. A streaming alternative to these approaches was proposed in JSR-173: Streaming API for XML (StAX). Its final release was published in March 2004 and it became part of JAXP 1.4 (to be included in the upcoming Java 6 release).

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.

w__w___w___.__jav___a2s_._c___o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.