Presentation is loading. Please wait.

Presentation is loading. Please wait.

SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.

Similar presentations


Presentation on theme: "SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design."— Presentation transcript:

1 SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design

2 SAX Parsing Introduction Review of XML Review of XML What is SAX parsing? What is SAX parsing? Simple Example program Simple Example program Compiler Design Issues Compiler Design Issues Demonstrated by a more complex example Demonstrated by a more complex example Wrap-up Wrap-up References References

3 Quick XML Review XML – Wave of the future XML – Wave of the future Method of representing data Method of representing data Differs from HTML by storing and representing data instead of displaying or formatting data Differs from HTML by storing and representing data instead of displaying or formatting data Tags similar to HTML tags, only they are user- defined Tags similar to HTML tags, only they are user- defined Follows a small set of basic rules Follows a small set of basic rules Stored as a simple ASCII text file, so portability is insanely easy Stored as a simple ASCII text file, so portability is insanely easy

4 Quick XML Review Syntax Syntax Every XML document has a preamble Every XML document has a preamble An XML document may or may not have a DTD (Document Type Definition) or Schema An XML document may or may not have a DTD (Document Type Definition) or Schema

5 Quick XML Review Syntax cont. Syntax cont. Every element has a start and end tag, with optional attributes Every element has a start and end tag, with optional attributes … … If an element does not contain any data (or elements) nested within, the closing tag can be merged with the start tag like so: If an element does not contain any data (or elements) nested within, the closing tag can be merged with the start tag like so:

6 Quick XML Review Syntax cont. Syntax cont. Elements must be properly nested Elements must be properly nested The outermost element is called the root element The outermost element is called the root element An XML document that follows the basic syntax rules is called well-formed An XML document that follows the basic syntax rules is called well-formed An XML document that is well-formed and conforms to a DTD or Schema is called valid An XML document that is well-formed and conforms to a DTD or Schema is called valid Once again, XML documents do not always require a DTD or Schema, but they must be well-formed Once again, XML documents do not always require a DTD or Schema, but they must be well-formed

7 Quick XML Review Sample XML files Sample XML files Catalog.xml Catalog.xml authorSimple.xml authorSimple.xml authorSimpleError.xml authorSimpleError.xml

8 What is SAX Parsing? Simple API for XML = SAX Simple API for XML = SAX SAX is an event-based parsing method SAX is an event-based parsing method We are all familiar with event-driven software, whether we know it or not We are all familiar with event-driven software, whether we know it or not Pop-up windows, pull-down menus, etc. Pop-up windows, pull-down menus, etc. If a certain “event” (or action) happens, do something If a certain “event” (or action) happens, do something A SAX parser reads an XML document, firing (or calling) callback methods when certain events are found (e.g. elements, attributes, start/end tags, etc.) A SAX parser reads an XML document, firing (or calling) callback methods when certain events are found (e.g. elements, attributes, start/end tags, etc.)

9 What is SAX Parsing? Benefits of SAX parsing Benefits of SAX parsing Unlike DOM (Document Object Model), SAX does not store information in an internal tree structure Unlike DOM (Document Object Model), SAX does not store information in an internal tree structure Because of this, SAX is able to parse huge documents (think gigabytes) without having to allocate large amounts of system resources Because of this, SAX is able to parse huge documents (think gigabytes) without having to allocate large amounts of system resources Really great if the amount of data you’re looking to store is relatively small (no waste of memory on tree) Really great if the amount of data you’re looking to store is relatively small (no waste of memory on tree) If processing is built as a pipeline, you don’t have to wait for the data to be converted to an object; you can go to the next process once it clears the preceding callback method If processing is built as a pipeline, you don’t have to wait for the data to be converted to an object; you can go to the next process once it clears the preceding callback method

10 What is SAX Parsing? Downside Downside Most limitations are the programmer’s problem, not the API’s Most limitations are the programmer’s problem, not the API’s SAX does not allow random access to the file; it proceeds in a single pass, firing events as it goes SAX does not allow random access to the file; it proceeds in a single pass, firing events as it goes Makes it hard to implement cross-referencing in XML (ID and IDREF) as well as complex searching routines Makes it hard to implement cross-referencing in XML (ID and IDREF) as well as complex searching routines

11 What is SAX Parsing? Callback Methods Callback Methods The SAX API has a default handler class built in so you don’t have to re-implement the interfaces every time ( org.xml.sax.helpers.DefaultHandler ) The SAX API has a default handler class built in so you don’t have to re-implement the interfaces every time ( org.xml.sax.helpers.DefaultHandler ) The five most common methods to override are: The five most common methods to override are: startElement(String uri, String lname, String qname, Attributes atts) startElement(String uri, String lname, String qname, Attributes atts) endDocument(String uri, String lname, String qname) endDocument(String uri, String lname, String qname) characters(char text[], int start, int length) characters(char text[], int start, int length) startDocument() startDocument() endDocument() endDocument()

12 Simple Example Program Sax.java Sax.java Instantiates a SAX parser and creates a default handler for the parser Instantiates a SAX parser and creates a default handler for the parser Reads in an XML document and echoes the structure to the standard out Reads in an XML document and echoes the structure to the standard out Two sample XML documents: Two sample XML documents: authorSimple.xml authorSimple.xml authorSimpleError.xml authorSimpleError.xml Demonstration here Demonstration here

13 Compiler Design Issues What is actually happening when a SAX parser parses an XML document? What is actually happening when a SAX parser parses an XML document? What type of internal data structures does it use? What type of internal data structures does it use? How do the callback methods fit in? How do the callback methods fit in? Can it solve problems of world peace, hunger, and death? (Or at least can it help me pass Compiler Design?) Can it solve problems of world peace, hunger, and death? (Or at least can it help me pass Compiler Design?) Demonstrated with SaxCatalogUnmarshaller example Demonstrated with SaxCatalogUnmarshaller example

14 Compiler Design Issues Heart of the Beast Heart of the Beast Underneath it all, the SAX parser uses a stack Underneath it all, the SAX parser uses a stack Whenever an element is started, a new data object is pushed onto the stack Whenever an element is started, a new data object is pushed onto the stack Later, when the element is closed, the topmost object on the stack is finished and can be popped Later, when the element is closed, the topmost object on the stack is finished and can be popped Unless it is the root element, the popped element will have been a child element of the object that now occupies the top of the stack (board) Unless it is the root element, the popped element will have been a child element of the object that now occupies the top of the stack (board)

15 Compiler Design Issues Heart of the Beast cont. Heart of the Beast cont. This process corresponds to the shift-reduce cycle of bottom-up parsers This process corresponds to the shift-reduce cycle of bottom-up parsers It is crucial that XML elements be well-formed and properly nested for this to work It is crucial that XML elements be well-formed and properly nested for this to work

16 Compiler Design Issues startElement() startElement() Four parameters: Four parameters: String uri = the namespace URI (Uniform Resource Identifier) String uri = the namespace URI (Uniform Resource Identifier) String lname = the local name of the element String lname = the local name of the element String qname = the qualified name of the element String qname = the qualified name of the element Attributes atts = list of attributes for this element Attributes atts = list of attributes for this element If the current element is a complex element, an object of the appropriate type is created and pushed on to the stack If the current element is a complex element, an object of the appropriate type is created and pushed on to the stack If the element is simple, a StringBuffer is pushed on to the stack, ready to accept character data If the element is simple, a StringBuffer is pushed on to the stack, ready to accept character data

17 Compiler Design Issues endElement() endElement() Three parameters: Three parameters: String uri = the namespace URI (Uniform Resource Identifier) String uri = the namespace URI (Uniform Resource Identifier) String lname = the local name of the element String lname = the local name of the element String qname = the qualified name of the element String qname = the qualified name of the element The topmost element on the stack is popped, converted to the proper type, and inserted into its parent, which now occupies the top of the stack (unless this is the root element – special handling required) The topmost element on the stack is popped, converted to the proper type, and inserted into its parent, which now occupies the top of the stack (unless this is the root element – special handling required)

18 Compiler Design Issues characters() characters() Three parameters: Three parameters: char text[] = character array containing the entire XML document char text[] = character array containing the entire XML document int start = starting index of current data in text[] int start = starting index of current data in text[] int length = ending index of current data in text[] int length = ending index of current data in text[] When the parser encounters raw text, it passes a char array containing the actual data, the starting position, and the length of data to be read from the array When the parser encounters raw text, it passes a char array containing the actual data, the starting position, and the length of data to be read from the array

19 Compiler Design Issues characters() cont. characters() cont. The implementation of the callback method inserts the data into the StringBuffer located on the top of the stack The implementation of the callback method inserts the data into the StringBuffer located on the top of the stack Can lead to confusion because of: Can lead to confusion because of: No guarantee that a single stretch of characters results in one call to characters() No guarantee that a single stretch of characters results in one call to characters() It stores all characters, including whitespace, encountered by the parser It stores all characters, including whitespace, encountered by the parser

20 Wrap-up SAX is an event-based parser, using callback methods to handle events found by the parser SAX is an event-based parser, using callback methods to handle events found by the parser Applications are written by extending the DefaultHandler class and overriding the event handler methods Applications are written by extending the DefaultHandler class and overriding the event handler methods The SAX parser usually uses a stack to perform operations The SAX parser usually uses a stack to perform operations And No, SAX will not save the world… And No, SAX will not save the world…

21 References Gittleman, Art. Advanced Java: Internet Applications (Second Edition). Scott Jones Publishers. El Granada, California. 2002. pp. 504-511. Janert, Phillip K. “Simple XML Parsing with SAX and DOM.” http://www.onjava.com/pub/a/onjava/2002/06/26/xml.html http://www.onjava.com/pub/a/onjava/2002/06/26/xml.html Published June 26, 2002. Accessed February 10, 2003. Wati, Anjini. “E-Catalog for a Small to Medium Enterprise.” http://ispg.csu.edu.au/subjects/itc594/reports/Tr-005.doc http://ispg.csu.edu.au/subjects/itc594/reports/Tr-005.doc Accessed February 10, 2003.


Download ppt "SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design."

Similar presentations


Ads by Google