Reading XML Streams

As described earlier in this chapter, the way you read XML streams with
a StAX processor, and what you get back, vary 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 attribute

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 XMLStreamReader.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 XMLStreamReader.getLocalName and XMLStreamReader.getText methods.

When the XMLStreamReader cursor is over a StartElement event, it reads the name and any attributes for the event, including
the namespace. All attributes for an event can be accessed using an index
value, and can also be looked up by namespace URI and local name. Note, however,
that only the namespaces declared on the current StartEvent are
available; previously declared namespaces are not maintained, and redeclared
namespaces are not removed.

XMLStreamReader Methods

XMLStreamReader provides the following methods for
retrieving information about namespaces and attributes:

Using XMLEventReader

The XMLEventReader API in the StAX event iterator
API provides the means to map events in an XML stream to allocated event objects
that can be freely reused, and the API itself can be extended to handle custom
events.

You can use the getAttributes method on the StartElement interface to use an Iterator over all the attributes
declared on that StartElement.

Reading Namespaces

Similar to reading attributes, namespaces are read using an Iterator created by calling the getNamespaces method
on the StartElement interface. Only the namespace for the
current StartElement is returned, and an application can
get the current namespace context by using StartElement.getNamespaceContext.