#!/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 is 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 example isn't very interesting, but it looks
good in print. The point is that we didn't have to
use YAWriter. We could have used any SAX handler Perl package on our
system, including ones we wrote ourselves, and tossed them into the
mix when baking a new XML::Generator::DBI object.
Given the same database table as the example above used, when the
$genenerator object's
execute method is called, it would act as if it
had just parsed the previous XML document (modulo the whitespace that
YAWriter inserted to make things more human-readable). It would act
this way even though the actual source isn't an XML
document at all, but a database table.