1,
SAX (Simple API for XML)
2, a SAX Parser functions implemented as a stream parser, with an event-driven API.The user defines a number of callback methods that will be called when events occur during parsing
3,Example
Given the following XML document:
<?xml version="1.0" encoding="UTF-8"?> <RootElement param="value"> <FirstElement> Some Text </FirstElement> <SecondElement param2="something"> Pre-Text <Inline>Inlined text</Inline> Post-text. </SecondElement> </RootElement>
This XML document, when passed through a SAX parser, will generate a sequence of events like the following:
- XML Processing Instruction, named xml, with attributes version equal to "1.0" and encoding equal to "UTF-8"
- XML Element start, named RootElement, with an attribute param equal to "value"
- XML Element start, named FirstElement
- XML Text node, with data equal to "Some Text" (note: text processing, with regard to spaces, can be changed)
- XML Element end, named FirstElement
- XML Element start, named SecondElement, with an attribute param2 equal to "something"
- XML Text node, with data equal to "Pre-Text"
- XML Element start, named Inline
- XML Text node, with data equal to "Inlined text"
- XML Element end, named Inline
- XML Text node, with data equal to "Post-text."
- XML Element end, named SecondElement
- XML Element end, named RootElement
In fact, this may vary: the SAX specification deliberately states that a given section of text may be reported as multiple sequential text events. Thus in the example above, a SAX parser may generate a different series of events, part of which might include:
- XML Element start, named FirstElement
- XML Text node, with data equal to "Some"
- XML Text node, with data equal to "Text"
- XML Element end, named FirstElement
4,Benefits
The quantity of memory that a SAX parser must use in order to function is typically much smaller than that of a DOM parser
5,
Drawbacks
XML Validation require access to the document in full
6.
XML Parser and APIs supporting SAX
- Crimson XML
- Fusion XML SAX Parser
- JAXP: Java API for XML Processing
- Expat: C SAX implementation.
- LibXML
- MSXML
- Xerces