background image

XMLStreamReader Methods

<< Using XMLStreamReader | Reading Attributes >>
<< Using XMLStreamReader | Reading Attributes >>
R
EADING
XML S
TREAMS
93
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:
XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader r = f.createXMLStreamReader( ... );
while(r.hasNext()) {
r.next();
}
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.