SAX - PowerPoint PPT Presentation

About This Presentation
Title:

SAX

Description:

Software that reads and parses XML. Passes data to the invoking application ... DOM (Document Object Model) Reads the entire document into memory in a tree structure ... – PowerPoint PPT presentation

Number of Views:192
Avg rating:3.0/5.0
Slides: 15
Provided by: witts
Category:
Tags: sax | dom

less

Transcript and Presenter's Notes

Title: SAX


1
SAX
  • A parser for XML Documents

2
XML 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

3
XML 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

4
XML 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

5
Simple API for XML
6
SAX 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.

7
SAX Parser
  • Which languages are supported?
  • Java
  • Perl
  • C
  • Python

8
SAX 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

9
SAX 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

10
SAX 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)
11
SAX Implementation in Java
  • Create a SAX Parser

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
12
SAX 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
13
SAX Implementation in Java
  • Other events generated by the parser
  • startDocument()
  • endDocument()
  • startElement()
  • endElement()
  • error()

14
For more information...
  • java.sun.com/xml
  • www.megginson.com/SAX
  • www.xml.com
  • www.ibm.com/developer/xml
Write a Comment
User Comments (0)
About PowerShow.com