background image

Cursor and Iterator APIs

<< Sample Iterator API Event Mapping | Comparing Cursor and Iterator APIs >>
<< Sample Iterator API Event Mapping | Comparing Cursor and Iterator APIs >>
C
HOOSING
B
ETWEEN
C
URSOR AND
I
TERATOR
API
S
87
opening and closing of elements, attribute order, document start and doc-
ument end, and so forth.
· As with proper XML syntax, all container elements have corresponding
start and end events; for example, every
StartElement
has a correspond-
ing
EndElement
, even for empty elements.
·
Attribute
events are treated as secondary events, and are accessed from
their corresponding
StartElement
event.
· Similar to
Attribute
events,
Namespace
events are treated as secondary,
but appear twice and are accessible twice in the event stream, first from
their corresponding
StartElement
and then from their corresponding
EndElement
.
·
Character
events are specified for all elements, even if those elements
have no character data. Similarly,
Character
events can be split across
events.
· The StAX parser maintains a namespace stack, which holds information
about all XML namespaces defined for the current element and its ances-
tors.
The
namespace
stack
is
exposed
through
the
javax.xml.namespace.NamespaceContext
interface,
and
can
be
accessed by namespace prefix or URI.
Choosing Between Cursor and Iterator
APIs
It is reasonable to ask at this point, "What API should I choose? Should I create
instances of
XMLStreamReader
or
XMLEventReader
? Why are there two kinds of
APIs anyway?"
Development Goals
The authors of the StAX specification targeted three types of developers:
·
Library and infrastructure developers
­ Create application servers, JAXM,
JAXB, JAX-RPC and similar implementations; need highly efficient, low-
level APIs with minimal extensibility requirements.
·
J2ME developers
­ Need small, simple, pull-parsing libraries, and have
minimal extensibility needs.