Parsing and processing
SAX
A streaming API that pushes events at your code — start element, characters, end element — as it reads, without building a tree.
SAX inverts control: you register handlers, the parser calls them, and memory use stays flat no matter how large the document is. It is how you process a document bigger than your machine, and it is the only sensible approach for a multi-gigabyte export.
You pay for that in state. There is no parent to walk up to and no way to look ahead, so any context you need — which element you are inside, what you have seen already — you have to track yourself.
- Defined in
- SAX 2.0 (de facto)
- Category
- Parsing and processing
- Also written
- Simple API for XML, push parsing
Why there is no demo
Nothing here to execute
SAX is a shape of API, not a property of a document. Nothing in a file distinguishes one that was parsed with SAX from one parsed into a tree, so there is no claim here that could be executed. What can be shown is on the DOM page: the cross-tree expression there is exactly what SAX cannot answer without you keeping the state yourself.What catches people out
- Character data can arrive in several callbacks for one run of text. Accumulate rather than assuming one call per text node.
- There is no going back. If you need an earlier element, you had to have kept it.
- SAX has no standard for XML namespaces beyond what the implementation gives you; check whether yours reports prefixes or expanded names.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- FormatsSitemap indexThe realistic case for streaming: a sitemap index pointing at fifty thousand-URL files.
- ToolsXML to CSV converterThe shape of job streaming suits — repeating records out of a large document.
Related terms
- DOMAn API that parses a whole document into a navigable tree in memory, where every node can be reached from any other.
- StAXA streaming API where your code pulls the next event from the parser, rather than the parser pushing events at your code.
- 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.