Delving Deeper Into StAX, Page 4
When to Use a Cursor or Event Iterator API
The cursor API is less verbose and less powerful than the event API. Presumably, it is more efficient at what it does and creates fewer temporary objects. Both the cursor and event iterator API are forward only API. However, the event iterator API provides a peek() feature to get the next event, as was demonstrated in SimpleXmlEventWriter. The event iterator API has many other capabilities that we didn't cover here, such as the ability to filter, buffer, persist, and compare events[i].
The sample code can be downloaded here.
StAX has cursor and event based iterator APIs. There are both reading and writing sides of the API. We reviewed code snippets of all the API, and built classes for reading and writing documents using the event API. We discussed why there are two types of StAX API, and why you might use each. Next time, we'll discuss when it is appropriate to use SAX, DOM, TrAX, and StAX—the various API in the JAXP family. Until then, the rest is up to you!
About the Author
Jeff Ryan is an enterprise architect for Hartford Financial Services. He has twenty years experience designing, developing, and delivering automated solutions to business problems. His current focus is on Java, XML, and Service Oriented Architecture. He may be reached at email@example.com.
[i] Java Community Process http://jcp.org, Streaming API for XML JSR-173 Specification Version 1.0, October 2003