Overview What is SAX Three steps to SAX Step1: Creating a custom object model Step2: Creating a SAX parser Step3: Creating a Document Handler Presentation layers for the object model Presentation Layer 1: Swing Presentation Layer 2: Servlet Source code XML 1.0 allows you to encode your information in textual form and it allows you to create tags which allow you to structure the information stored in XML documents.This information must at some point be read by some program to do something useful, like viewing, modifying or printing it.
For Java, these parsers include the Sun TR2 XML Parser, Datachannel XJ2, IBM XML Parser for Java, and Open XML (among many others).
All of these parsers implement the SAX API (and also the DOM API).
There are fewer differences in the implementation of SAX compared to the implementation of DOM 1.0 (simply because SAX is so much smaller and simpler than DOM).
So your Java programs must use an XML Parser written in Java that implements the SAX API in order for you to use SAX.
In this tutorial, I illustrate the use of the Sun TR2 Parser, and I will show you how you can modify the code very simply in order to use the XML parser of your choice. Unlike DOM (Document Object Model) which creates a tree based representation for the information in your XML documents, SAX does not have a default object model.
This means that when you create a SAX parser and read in a document (unlike DOM) you will not be given a nice default object model.
A SAX parser is only required to read in your XML document and fire events based on the things it encounters in your XML document.
Events are fired when the following things happen: We will start by looking at the open and close element tag events and the #PCDATA and CDATA events.
One thing to remember about SAX is that the sequence of these events is very important, because the sequence in which events are fired determines how you will have to interpret each event.
Since SAX does not come with a default object model representation for the data in your XML document, the first thing you have to when using SAX is create your own custom Java object model for your data.
This could be something as simple as creating an Address Book class if your XML document is an address book.