background image

XMLStreamReader Methods

<< Resources, Namespaces, and Errors | Using XMLEventReader >>
<< Resources, Namespaces, and Errors | Using XMLEventReader >>

XMLStreamReader Methods

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:
int getAttributeCount();
String getAttributeNamespace(int index);
String getAttributeLocalName(int index);
String getAttributePrefix(int index);
String getAttributeType(int index);
String getAttributeValue(int index);
String getAttributeValue(String namespaceUri, String localName);
boolean isAttributeSpecified(int index);
Namespaces can also be accessed using three additional methods:
int getNamespaceCount();
String getNamespacePrefix(int index);
String getNamespaceURI(int index);
Instantiating an XMLStreamReader
This example, taken from the StAX specification, shows how to instantiate an input factory,
create a reader, and iterate over the elements of an XML stream:
Using StAX
The Java EE 5 Tutorial · September 2007
562