Streaming API for XML (JSR 173)
Version 1.0

Streaming API for XML (JSR 173) 1.0

This is the documentation for the Streaming API for XML (JSR 173).

See:
          Description

Packages
javax.xml.stream Provides an API for processing XML as a stream
javax.xml.stream.events Describes a set of events for processing XML
javax.xml.stream.util Provides various utilities for processing XML

 

This is the documentation for the Streaming API for XML (JSR 173).  This API specifies a bi-directional API for reading and writing XML.   The Streaming API for XML is designed to serve two basic uses. The first allows users to read and write XML as efficiently as possible (cursor API). The second is designed to be easy to use, event based, easy to extend, and allow easy pipelining (iterator API). The iterator API is intended to layer ontop of the cursor API.

The cursor API has two interfaces: XMLStreamReader and XMLStreamWriter. The iterator API has two main interfaces: XMLEventReader and XMLEventWriter

A factory is used to instantiate instances of the interfaces for example the following code instantiates a factory and uses it to get an instance of an XMLStreamReader:

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader =
   inputFactory.createXMLStreamReader(new FileInputStream("inFile.xml"));
while(reader.hasNext()) {
   reader.next();
   // insert your processing here
}
 

To write XML, an XMLOutputFactory can be instantiated: 

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer =
   outputFactory.createXMLStreamWriter(new FileOutputStream("outFile.xml"));
writer.writeStartElement("doc");
writer.writeAttribute("att","value");
writer.writeCharacters("some text");
writer.writeEndElement();

 

outFile.txt will contain the following XML:

<doc att="value">some text</doc>

The same factory can be used to create instances of the XMLEventReader:

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader reader =
   inputFactory.createXMLEventReader(new FileInputStream("inFile.xml"));
while(reader.hasNext()) {
   XMLEvent e = reader.nextEvent();
   System.out.println("Event:["+e+"]");    // insert your processing here
}
 


Streaming API for XML (JSR 173)
Version 1.0