XML technologies / XML / Perl & XML / SAX
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
XML::Parser has proved remarkably effective as a parser and event-stream generator for many purposes. The future of Perl and XML, however, lies in other parsers. We do not want to be tied to one parser for every possible purpose, but to choose the most suitable one for each situation. One parser may be written in Perl for portability, another in C for speed. A pseudoparser might even read a completely different input format and convert it into an XML stream. It is impossible to predict everything a parser might need to do. Even XML::Parser with its countless options and modes eventually reaches its limits. The future lies in a diversity of parsers.
An environment with many parsers needs a degree of consistency. If each parser had its own interface, developers would eventually lose their minds. It is better to learn one interface and distinguish conforming from nonconforming parsers than to learn a hundred ways of doing the same thing. We need a standard interface between source code and parser: a universal, flexible, reliable connection free from the quirks of a particular parser.
An event-oriented interface called SAX has become established among XML developers. SAX grew out of discussions on the XML-DEV mailing list. A faithful shepherd named David Megginson, who maintains a website about SAX, quickly turned those discussions into a useful specification. The first incarnation, SAX Level 1 or SAX1, supports elements, attributes, and PIs, but not Namespaces or CDATA sections. This led to the next step, SAX2, whose interface covers practically everything one might expect from generic XML.
Note: Two further extensions have since appeared, sometimes called “SAX2 Extensions” or “SAX 2.0.2.” See Megginson’s website for details.
SAX is a huge success. Its simplicity makes the interface quick to learn and easy to use. In its early days, XML was closely tied to Java, so SAX was originally a pure Java interface. An interface is a special kind of class in which methods are only declared, leaving implementation entirely to the developer. This is ideal for an interface such as SAX.
Enthusiasm for SAX quickly spread to the Perl community, and the first implementations appeared on CPAN. There was a problem, however: Perl does not enforce requirements as rigorously as Java. Type safety is almost absent, and the language tolerates nearly any inconsistency. Java checks function argument types against the interface at compile time, while everything is still under the programmer’s control. Perl accepts arbitrary arguments, even before the program leaves the programmer’s care, and certainly afterward. Defining a standard in Perl is therefore primarily a matter of words. Implementation depends much more on the programmer’s understanding, experience, and vigilance.
One of the first SAX implementations in Perl was the module XML::Parser::PerlSAX by Ken McLeod. It is a subclass of XML::Parser, converting the event stream produced by Expat into a stream of SAX events.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top