Java Reference
In-Depth Information
Streaming parsers can work with documents whose format is only loosely known, but you do
need to know what you want to work with beforehand: you have to tell the parser what you
want to pull.
But the infosets that StAX creates are very small, and are immediate candidates for garbage
collection. This gives your XML processing work a small footprint, making it ideal for use
not just with small heap devices such as mobile phones, but with long-running server-side ap-
plications too.
Unlike SAX, StAX is able to write XML documents. This reduces the number of APIs you
have to deal with. That having been said, in addition to reading and writing XML data, there
are two different models for parsing data with StAX: the cursor model and the iterator model.
Using the StAX cursor model: XMLStreamReader
The
XMLStreamReader
interface does the heavy lifting here. Using this interface, you can read
everything about both the structure of a document and its content in a stream of events. To
receive these events, use the
hasNext
method to determine if there are any more events to
read, and the
next
method to get an integer token for the next event. Using that token, you
switch on the different kinds of parse events, and do some work if the current event represents
something in which you're interested.
You can capture events for the following
XMLEvent
subinterfaces, each representing a differ-
ent aspect of a document's structure:
▪
CDATA
▪
CHARACTER
▪
COMMENT
▪
DTD
▪
START_DOCUMENT
▪
END_DOCUMENT
▪
START_ELEMENT
▪
END_ELEMENT
▪
ENTITY_DECLARATION
▪
NAMESPACE
▪
NOTATION_DECLARATION
▪
PROCESSING_INSTRUCTION