background image

Using XMLStreamReader

<< Resources, Namespaces, and Errors | XMLStreamReader Methods >>
<< Resources, Namespaces, and Errors | XMLStreamReader Methods >>
92
S
TREAMING
API
FOR
XML
Error Reporting and Exception Handling
All fatal errors are reported by way of
javax.xml.stream.XMLStreamExcep-
tion
.
All
nonfatal
errors
and
warnings
are
reported
using
the
javax.xml.stream.XMLReporter
interface.
Reading XML Streams
As described earlier in this chapter, the way you read XML streams with a StAX
processor--and more importantly, what you get back--varies significantly
depending on whether you are using the StAX cursor API or the event iterator
API. The following two sections describe how to read XML streams with each of
these APIs.
Using XMLStreamReader
The
XMLStreamReader
interface in the StAX cursor API lets you read XML
streams or documents in a forward direction only, one item in the infoset at a
time. The following methods are available for pulling data from the stream or
skipping unwanted events:
·
Get the value of an at
tribute
· Read XML content
· Determine whether an element has content or is empty
· Get indexed access to a collection of attributes
· Get indexed access to a collection of namespaces
· Get the name of the current event (if applicable)
· Get the content of the current event (if applicable)
Instances of
XMLStreamReader
have at any one time a single current event on
which its methods operate. When you create an instance of
XMLStreamReader
on
a stream, the initial current event is the
START_DOCUMENT
state.The
XMLStream-
Reader.next()
method can then be used to step to the next event in the stream.
Reading Properties, Attributes, and Namespaces
The
XMLStreamReader.next()
method loads the properties of the next event in
the stream. You can then access those properties by calling the
XMLStream-
Reader.getLocalName()
and
XMLStreamReader.getText()
methods.