XML technologies / XML / Perl & XML / SAX / XML::Handler::YAWriter as a handler base class

(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)

The module XML::Handler::YAWriter (“ Yet Another Writer”) by Michael Koehne also works well for XML output. It is another typical base class for all kinds of work based on SAX.

If you have worked with one of the many Tie::* base classes, the idea is similar. You start with a class defining all the callbacks, although these do little or nothing useful. Their presence provides all methods a SAX handler needs. Your own class implements only methods requiring special behavior; the others are ignored.

Here, the base class offers something extra: access to an array of strings stored as an instance variable in the handler object. It collects the strings from which the SAX events were derived. This is unremarkable when the input was XML, but very useful when a generator produces events from a different kind of data source. It is an extremely simple way to convert a non-XML source to XML and save it to disk.

The drawback of a base class is that you must never forget to call $self->SUPER::[methodenname] in every handler method. Otherwise, you disrupt the base class’s internals and may carelessly tear whole chunks out of the XML document.

  

  

 

 

 

back to top