background image

Cursor API

<< StAX-Enabled Clients | Iterator Event Types >>
<< StAX-Enabled Clients | Iterator Event Types >>

Cursor API

Cursor API
As the name implies, the StAX cursor API represents a cursor with which you can walk an XML
document from beginning to end. This cursor can point to one thing at a time, and always
moves forward, never backward, usually one infoset element at a time.
The two main cursor interfaces are XMLStreamReader and XMLStreamWriter.
XMLStreamReader
includes accessor methods for all possible information retrievable from the
XML Information model, including document encoding, element names, attributes,
namespaces, text nodes, start tags, comments, processing instructions, document boundaries,
and so forth; for example:
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
}
You can call methods on XMLStreamReader, such as getText and getName, to get data at the
current cursor location. XMLStreamWriter provides methods that correspond to StartElement
and EndElement event types; for example:
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
}
The cursor API mirrors SAX in many ways. For example, methods are available for directly
accessing string and character information, and integer indexes can be used to access attribute
and namespace information. As with SAX, the cursor API methods return XML information as
strings, which minimizes object allocation requirements.
Iterator API
The StAX iterator API represents an XML document stream as a set of discrete event objects.
These events are pulled by the application and provided by the parser in the order in which they
are read in the source XML document.
StAX API
Chapter 18 · Streaming API for XML
553