Simple API for XML (SAX) and the Document Object Model (DOM)

This chapter is based on Chapter 8 of Erik Ray's book [Ray 2001], Chapters 8 and 9 of the Deitel, et.al. tome [Deitel 2001], plus additional material from the web.

Introduction

XML documents, when parsed, are represented as a hierarchical tree structure. SAX and DOM are two dramatically different APIs for accessing information in XML documents.

Simple API for XML (SAX)

SAX was developed by the W3C and released in 1998. SAX-based parsers invoke methods when markup (e.g. a start tag, an end tag, etc.) is encountered. No tree structure is created - data is passed to the application from the XML document as it is found. SAX parsers are typically used for reading XML documents that will not be modified.

SAX-based parsers are available for a variety of programming languages; C++, Java, and Perl being the most popular. SAX is based on an event-driven model using call-backs to handle processing. Consult Figure 1 for what an event-driven program can and can't do.

Can do Can't do
  • Search a document for an element containing a keyword
  • Print out formatted content
  • Modify an XML document by making small changes, such as fixing spelling and renaming elements
  • Read data to build a complex data structure
  • Re-order the elements in a document
  • Resolve cross-references between elements
  • Verify ID-IDREF links
  • Validate an XML document

Figure 1 : Event-driven Programming

SAX parsing is fast with low memory consumption, making it ideal for processing XML on the server side, e.g. to translate XML into HTML for viewing in a traditional web browser. Figure 2 illustrates the events that occur when SAX parsing our CD collection document.

  1. Found start tag: cdcollection
  2. Found start tag: album
  3. Found attribute: id="540 590-2"
  4. Found start tag: title
  5. Found text: Sheryl Crow
  6. Found end tag: title
  7. Found start tag: artist
  8. Found text: Sheryl Crow
  9. Found end tag: artist
  10. Found start tag: label
  11. Found text: A & M Records
  12. Found end tag: label
  13. Found start tag: track
  14. Found attribute: time="4:56"
  15. Found text: maybe angels
  16. Found end tag: track
  17. ...

Figure 2 : CD Events

SAX forgets events as quickly as they are generated, but it can be used as the basis of a more complex API, such as DOM (see below). The three most popular SAX parsers are Xerces [Xerces] from the Apache Project, JAXP [JAXP] from Sun Microsystems, and MSXML [MSXML] from Microsoft. In fact, all three support both SAX and DOM.

Document Object Model (DOM)

The W3C provides a standard recommendation for building a tree structure in memory for XML documents called the XML Document Object Model (DOM). A DOM-based parser exposes (i.e. makes available) a library, called the DOM Application Programming Interface (API), that allows data in an XML document to be accessed and modifyed by manipulating the nodes in a DOM tree, such as our CD collection tree.

Actually programming a DOM-based parser in a language such as Java or Perl is beyond the scope of this chapter. However, manipulating a DOM tree using XSL transformations is the subject of a later chapter. Further details concerning the DOM can be found on the W3C web site [W3C] or their training site [W3Schools].

References

  1. [an error occurred while processing this directive]
  2. JAXP, http://java.sun.com/xml/
  3. MSXML 3.0, http://msdn.microsoft.com/xml/
  4. [an error occurred while processing this directive]
  5. W3C DOM Home Page, http://www.w3.org/DOM/
  6. W3Schools DOM Introduction, http://www.w3schools.com/dom/
  7. Xerces, http://xml.apache.org