Parsing and processing
StAX
A streaming API where your code pulls the next event from the parser, rather than the parser pushing events at your code.
StAX keeps SAX's flat memory profile and gives control back to the caller. Your loop asks for the next event, which means you can stop early, hand the cursor to another function, or switch strategies partway through a document — none of which is natural in a callback model.
It is the usual choice in the JVM world, and the same pull-parser shape appears elsewhere under other names. Between it and SAX the trade is stylistic more than practical: both stream, neither can look backwards.
- Defined in
- JSR 173
- Category
- Parsing and processing
- Also written
- Streaming API for XML, pull parsing
What catches people out
- Stopping early is StAX's real advantage: you can abandon a document after the element you wanted, which SAX cannot do cleanly.
- Cursor and event-iterator APIs coexist. The cursor is faster; the iterator is easier to pass around.
- As with SAX, there is no backtracking. Keep what you will need.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
Related terms
- SAXA streaming API that pushes events at your code — start element, characters, end element — as it reads, without building a tree.
- DOMAn API that parses a whole document into a navigable tree in memory, where every node can be reached from any other.
- ParserThe component that reads XML text and reports its structure to an application, or reports a fatal error and stops.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.