Opentopia Directory Encyclopedia Tools

Stax

Encyclopedia : S : ST : STA : Stax


Streaming API for XML (StAX) is an application programming interface (API) to read and write XML documents in the Java programming language.

Traditionally, XML APIs are either:

Both have advantages; the former (for example, DOM) allows for random access to the document, the latter (e.g. SAX) requires a small memory footprint so is typically much faster.

These two access metaphors can be thought of as polar opposites. A tree based API allows unlimited, random, access and manipulation, while an event based API is a 'one shot' pass through the source document.

StAX was designed as a median between these two opposites. In the StAX metaphor, the programmatic entry point is a cursor that represents a point within the document. The application moves the cursor forward - 'pulling' the information from the parser as it needs. This is different from an event based API - such as SAX - which 'pushes' data to the application - requiring the application to maintain state between events as necessary to keep track of location within the document.

Origins

StAX has its roots in a number of incompatible pull APIs for XML, most notably XMLPULL, the authors of which (Stefan Haustein and Aleksandr Slominski) collaborated with, amongst others BEA, Oracle, Sun, Breeze Factor and James Clark.

Examples

From JSR-173 Specification• Final, V1.0 (used under fair use).

Quote:

The following Java API shows the main methods for reading XML in the cursor approach.
// Java 
public interface XMLStreamReader  
The writing side of the API has methods that correspond to the reading side for “StartElement” and “EndElement” event types.
// Java 
public interface XMLStreamWriter  
5.3.1 XMLStreamReader
This example illustrates how to instantiate an input factory, create a reader and iterate over the elements of an XML document.
XMLInputFactory f = XMLInputFactory.newInstance(); 
XMLStreamReader r = f.createXMLStreamReader(... ); 
while (r.hasNext()) 

Implementations

See also

Competing and complementary ways to parse XML in Java:

External links

Download JSR specification document as a pdf here: [download now]
  • [www.extreme.indiana.edu/~aslom/xmlpull/] Articles and resources from Aleksander Slominski.
  • * [~aslom/xmlpull/patterns.html] Article on XML Pull (and StAX) design patterns (recommended).
  • [XMLPull.org]
  • [StAX and Sax comparison.]
  • [StAX and Java eg. from DevX.com]
  •  


    From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
    All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

    Search Titles
    0123456789
    ABCDEFGHIJ
    KLMNOPQRST
    UVWXYZ?

    E-mail this article to:

    Personal Message: