XML technologies / XML / Perl & XML / RSS, SOAP and other XML applications / XML-based programming aids

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

Next, consider software that reverses the previous approach. Rather than conveniently manipulating XML in Perl style, we use XML standards to simplify a task that could work without XML. Recently, key members of the perl-xml mailing list discussed a small general-purpose Perl data platform, with SAX at its core for performance. Interesting, useful results included XML::SAXDriver::Excel and XML::SAXDriver::CSV by Ilya Sterin, and Matt Sergeant’s XML::Generator::DBI. All three wrap foreign formats—Microsoft Excel files, CSV delimited files and SQL databases—in SAX interfaces. We saw an example in SAX. Programmers see sources as simple and maintainable as XML. Whatever contortions the modules use to create that illusion need not concern users.

Because this has profound implications for newer developments, let us examine one module closely.

XML::Generator::DBI

XML::Generator::DBI is an excellent example of a glue module, or middleware: simple software joining two otherwise unrelated but thematically connected programs. Once created, its object controls others, here a DBI database handle and SAX ContentHandler.

XML::Generator::DBI knows nothing of the original data source, relying only on standard DBI, SAX or SAX2 calls. Calling the method execute on the XML::Generator::DBI object with an ordinary SQL statement makes it behave like a normal DBI handle.

The next example uses a handler instance of Michael Koehne’s XML::Handler::YAWriter, a highly configurable SAX-to-text module. It converts, for example, a CD table to well-formed XML and outputs it:

#!/usr/bin/perl

use warnings;
use strict;

use XML::Generator::DBI;
use XML::Handler::YAWriter;                      

use DBI;

my $ya = XML::Handler::YAWriter->new(AsFile => "-");
my $dbh = DBI->connect("dbi:mysql:dbname=test", "jmac", "");
my $generator = XML::Generator::DBI->new(
                      Handler => $ya,
                      dbh => $dbh
                      );
my $sql = "select * from cds";

$generator->execute($sql);

The result looks like this:

<?xml version="1.0" encoding="UTF-8"?><database>
    <select query="select * from cds">
        <row>
            <id>1</id>
            <artist>Donald and the Knuths</artist>
            <title>Greatest Hits Vol. 3.14159</title>
            <genre>Rock</genre>
        </row>
        <row>
            <id>2</id>
            <artist>The Hypnocrats</artist>
            <title>Cybernetic Grandmother Attack</title>
            <genre>Electronic</genre>
        </row>
        <row>
            <id>3</id>
            <artist>The Sam Handwich Quartet</artist>
            <title>Handwich a la Yogurt</title>
            <genre>Jazz</genre>
        </row>
    </select>
</database>

This alone is unexciting, though the output looks good. The point is that YAWriter is not required: any SAX handler, especially your own, works. With the same table, calling the method execute on the object $generator behaves as if an XML parser had read the displayed document and generated events, apart from whitespace YAWriter adds for readability. A document and a database table are indistinguishable as sources.

More on DBI and SAX

Let us explore their relationship further: they have more in common than first appears.

DBI’s success rests on architecture. Normally two packages are needed: DBI.pm implements the public interface and documentation, but cannot access a database alone. A database-specific DBD driver is also required. CPAN has many, including DBD::MySQL, DBD::Oracle or DBD::Pg for Postgres. Programmers see only DBI, sending SQL and receiving tables. Drivers communicate with databases through an internal, still database-independent but lower-level interface that only driver authors need. In theory, changing the driver changes the database without changing the Perl program.

Note: This assumes avoiding proprietary database features. $sth->query('select last_insert_id( ) from foo') works well with MySQL, but would horrify PostgreSQL users. An excellent appendix discusses this in Programming with Perl DBI by Alligator Descartes and Tim Bunce (O’Reilly).

Perl/XML is moving similarly. What began in 2001 with the generators discussed earlier continues with the module XML::SAX. XML::SAX is a SAX2 abstraction like DBI. Request a parser, with options specifying its behavior, and a suitable one is found, instantiated and returned automatically. Attach any SAX handler, just as with XML::Generator::DBI, and you are done.

Of course, XML::SAX does not supply database drivers; that is already handled by XML::Generator::DBI. Instead, PerlSAX handlers obtain data from almost any source; a DBI driver is one example. As with DBI, one skilled hacker can implement access once, and everyone else can simply reuse it. No knowledge of the source is needed, only SAX, to process the formerly foreign format.

  

  

 

 

 

back to top