Parsing XML Using SAX
IN THIS CHAPTER
• What Is SAX, Anyway?
• What SAX Is Not
• Why Do I Need SAX?
• SAX vs. DOM
• SAX Versions
• SAX Basics
• Working with SAX
In the last chapter, you saw how XML data could be parsed using the Document Object Model (DOM). In this chapter, we will explore another tool for parsing XML—the Simple API for XML (SAX). DOM can also be used for creating documents. Unlike DOM, SAX can only be used for parsing existing documents. We will look at some of the reasons why SAX is preferred for certain applications. We will explore several exam-ples that demonstrate how the SAX API works, and finally we will look at a practical example as a case study using SAX.
SAX has an interesting development history. Many of the APIs commonly used today (even outside the XML community) have been developed through some kind of formal process. A standards body, such as the W3C, is often involved. SAX, on the other hand, was the result of collaboration by the members of the XML-DEV mailing list, principally Dave Megginson. The home of SAX is Dave Megginson’s site: http://www.megginson. com/SAX. The history of SAX can be found at http://www.megginson.com/SAX/SAX1/ history.html, and the XML-DEV mailing list is located at http://www.xml.org/xml/ xmldev.shtml.
Despite the lack of a formal standards body guiding the development of SAX, it has rapidly become one of the most popular APIs for parsing XML. This is due, in part, to the fact that it is available in the public domain, free of charge, in both source and binary form. The copyright is probably the least restrictive of any available today. Unlike the GNU Public License (GPL) and others, the SAX copyright is just one short paragraph:
SAX2 is Free!
I hereby abandon any property rights to SAX 2.0 (the Simple API for XML), and release all of the SAX 2.0 source code, compiled code, and documentation con-tained in this distribution into the Public Domain. SAX comes with NO WAR-RANTY or guarantee of fitness for any purpose.
David Megginson, firstname.lastname@example.org
Basically, you can do whatever you want with SAX—there are no strings attached. Then again, there is no formal support. Documentation is limited to the API and source code. One might argue that you get what you pay for, but SAX has been around for several years and the quality is quite high. As with any open-source software, if you run into a problem, you can dive in and fix it yourself. In many cases, this is more effective than high-priced technical support from a large corporation.