background image

Choosing between Cursor

<< Iterator API Event Mapping | Development Goals >>
<< Iterator API Event Mapping | Development Goals >>

Choosing between Cursor

TABLE 18­3
Example of Iterator API Event Mapping
(Continued)
#
Element/Attribute
Event
14
isCData = false
data =
"\n"
IsWhiteSpace = true
Characters
15
qname = Book
namespaces = null
EndElement
16
isCData = false
data =
"\n"
IsWhiteSpace = true
Characters
17
qname = BookCatalogue:http://www.publishing.org
namespaces = {BookCatalogue
" -> http://www.publishing.org"}
EndElement
18
EndDocument
There are several important things to note in this example:
The events are created in the order in which the corresponding XML elements are
encountered in the document, including nesting of elements, opening and closing of
elements, attribute order, document start and document end, and so forth.
As with proper XML syntax, all container elements have corresponding start and end
events; for example, every StartElement has a corresponding 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 ancestors. The namespace stack, which
is exposed through the javax.xml.namespace.NamespaceContext interface, 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?"
StAX API
Chapter 18 · Streaming API for XML
557