Title: SAX
1SAX
- A parser for XML Documents
2XML Parsers
- What is an XML parser?
- Software that reads and parses XML
- Passes data to the invoking application
- The application does something useful with the
data
3XML Parsers
- Why is this a good thing?
- Since XML is a standard, we can write generic
programs to parse XML data - Frees the programmer from writing a new parser
each time a new data format comes along
4XML Parsers
- Two types of parser
- SAX (Simple API for XML)
- Event driven API
- Sends events to the application as the document
is read - DOM (Document Object Model)
- Reads the entire document into memory in a tree
structure
5Simple API for XML
6SAX Parser
- When should I use it?
- Large documents
- Memory constrained devices
- When should I use something else?
- If you need to modify the document
- SAX doesnt remember previous events unless you
write explicit code to do so.
7SAX Parser
- Which languages are supported?
- Java
- Perl
- C
- Python
8SAX Parser
- Versions
- SAX 1 introduced in May 1998
- SAX 2.0 introduced in May 2000 and adds support
for - namespaces
- filter chains
- querying and setting properties in the parser
9SAX Parser
- Some popular SAX APIs
- Apache XML Project Xerces Java Parser
http//xml.apache.org/xerces-j/index.html - IBMs XML for Java (XML4J) http//www.alphaworks.i
bm.com/formula/xml - For a complete list, see http//www.megginson.com/
SAX
10SAX Implementation in Java
- Create a class which extends the SAX event handler
Import org.xml.sax. import org.xml.sax.helpers.P
arserFactory Public class SaxApplication
extends HandlerBase public static void
main(String args)
11SAX Implementation in Java
public static void main(args) String
parserName org.apache.xerces.parsers.SAXParser
try SaxApplication app new
SaxApplication() Parser parser
ParserFactory.makeParser(parserName) parser.set
DocumentHandler(app) parser.setErrorHandler(app
) parser.parse(new InputSource(args0))
catch (Throwable t) // Handle exceptions
12SAX Implementation in Java
- Override event handlers of interest
Public class SaxApplication extends HandlerBase
public void main (String args) // stuff
missing public void startElement(String
name, AttributeList attrs) // Process this
element
13SAX Implementation in Java
- Other events generated by the parser
- startDocument()
- endDocument()
- startElement()
- endElement()
- error()
14For more information...
- java.sun.com/xml
- www.megginson.com/SAX
- www.xml.com
- www.ibm.com/developer/xml