StAX

Short description: Application programming interface

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

Traditionally, XML APIs are either:

* DOM based - the entire document is read into memory as a tree structure for random access by the calling application
* event based - the application registers to receive events as entities are encountered within the source document.

Both have advantages: DOM, for example, allows for random access to the document, and event driven algorithm like SAX has a small memory footprint and 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.

## Contents

* 1 Origins
* 2 Examples
* 3 See also
* 4 External links

## Origins

StAX has its roots in a number of incompatible pull APIs for XML, most notably XMLPULL, the authors of which (Stefan Haustein and Aleksander Slominski) collaborated with, amongst others, BEA Systems, Oracle, Sun 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.

public interface XMLStreamReader {
public int next() throws XMLStreamException;
public boolean hasNext() throws XMLStreamException;
public String getText();
public String getLocalName();
public String getNamespaceURI();
// ...other methods not shown

The writing side of the API has methods that correspond to the reading side for “StartElement” and “EndElement” event types.

public interface XMLStreamWriter {
public void writeStartElement(String localName) throws XMLStreamException;
public void writeEndElement() throws XMLStreamException;
public void writeCharacters(String text) throws XMLStreamException;
// ...other methods not shown

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 xmlInputFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(...);
while (xmlStreamReader.hasNext()) {
xmlStreamReader.next();

## See also

Competing and complementary ways to process XML in Java (the order is loosely based on initial date of introduction):

* Document Object Model (DOM), the first standardized, language/platform-independent tree-based XML processing model; alternate Java tree models include JDOM, Dom4j, and XOM
* Simple API for XML (SAX), the standard XML push API
* Java XML Binding API (JAXB), works on top of another parser (usually streaming parser), binds contained data to/from Java objects.
* Streaming XML
* XQuery API for Java

## External links

* Introduction to StAX XML.com, Harold, Elliotte Rusty
* Java Streaming API for XML (Stax) - Tutorial
* XMLPull Patterns Article on XML Pull (and StAX) design patterns by Aleksander Slominski.
* StAX Parser - Cursor & Iterator APIs Article on Cursor & Iterator APIs by HowToDoInJava.

* v
* t
* e

Java EE 7 specifications

Web app|

* Servlet
* JavaServer Pages
* Expression Language
* JSTL
* JavaServer Faces
* WebSocket

Enterprise app|

* Dependency injection
* Bean Validation
* Enterprise JavaBeans
* JPA
* JMS
* JTA
* JavaMail
* JCA
* Common Annotations

Web services|

* JAX-RS
* JAX-WS
* Web Services Metadata
* Java API for XML Messaging
* JAXR

APM|

* J2EE Management

Related|

* JAXB
* JAXP
* JDBC
* JMX
* JavaBeans Activation Framework
* Streaming API for XML

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/StAX. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=StAX&oldid=246105"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template