I am trying to follow the recommended way of parsing XML with StAX
using sun's Cursor-to-Event Example for Java EE 5. You are supposed to traverse the XML via the Cursor API and allocate an XMLEvent
using an XMLEventAllocator
when necessary.
Awkwardly, sun's own example does not compile (at least not with JDK 1.6, even with 1.5 code compliance). The example tries to instantiate an allocator via new
, but the according implementation classes in the JDK are not accessible externally.
After reading the JavaDocs and searching the web I have found literally nothing.
One could implement the XMLEventAllocator
interface from scratch, but it seems really wrong, when there are perfectly good implementations in the JDK, besides not being an expert in StAX
makes it difficult to get it right.
I would not use that example as a best practice for using StAX. With StAX you have two approaches XMLStreamReader and XMLEventReader. Both give you an API for accessing the events for a depth-first traversal of an XML document. With XMLStream reader you can request info from the XMLStreamReader based on the event type, and with XMLEventReader you are given objects representing the original event.
I recommend using the XMLStreamReader API directly.