background image

Iterator API

<< StAX API | Iterator Event Types >>
<< StAX API | Iterator Event Types >>
82
S
TREAMING
API
FOR
XML
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, pro-
cessing 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 alloca-
tion 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.
The base iterator interface is called
XMLEvent
, and there are subinterfaces for
each event type listed in Table 4­2, below. The primary parser interface for read-