Monday, 20 December 2010

Simple API for XML (SAX)

Evaluate SAX (the XML API, giving examples of usage)

# SAX stands for Simple API for XML. It is a sequential access parser API for XML. It provides a mechanism for reading data from an XML document. It is callback implementation. Some open source projects like Apache and also some commercial users like Sun, IBM, and Microsoft uses it broadly. SAX is known for being the most efficient standard way to process XML, far from the easiest. It is a java base API and is very robust and effective.  Likewise DOM, it also allows the access of information in an XML document, but its different form DOM. When using DOM, the DOM object is instantiated, the XML document is loaded and the elements and attributes are accessed as needed from the data tree. The document can be transformed, added to it and can be output in any style. However, the SAX is event driven; the full XML document doesn’t load at the starting. Instead, it loads part by part, in sequence. An even executes at each stage as the section developments. For instance, consider the following XML code:
<?xml version=“1.0” ?>
<Student>
            <name>Kshitij Munankami</name>
</Student>

The parse steps and executes in following events:
startDocument
startElement: Student
startElement: name
character: Kshitij Munankami
endElement: name
endElement: Student
endDocument
In compare to DOM, it requires a bit more work for setting up a program. As SAX is an event-driven method, it is harder to visualize. Also back up to an earlier part or rearrangement of the document is not allowed in SAX. However, even if we plan to build DOM application, there are lots of important reasons that we should know about SAX model:
  •  Same error handling: Both SAX and DOM APIs generate similar expectations, so the error handling code is vertically identical.
  •  Handling validation errors: By default, the specifications want that validation errors ignored. So, we should have the knowledge of how SAX error handling works, if wants to throw an exception in the event of a validation error. 
  • Converting existing data: There is some mechanism to convert a data to an XML. So, take benefits of that mechanism requires knowledge of the SAX model.

No comments:

Post a Comment