Chapter 5 — Storing and Sharing Information 91
Listing 5-29: Generating XML from Your SQL Database
#!/usr/bin/perl
use DBI;
use XML::Generator;
my $dbh = DBI-
connect(“DBI:mysql:database=google_maps;host=mysql.mcslp.pri”,’maps’,’maps’);
if ($dbh)
{
print “Connected to database\n”;
}
else
{
print “Couldn’t connect\n”;
}
my $sth = $dbh->prepare(“select * from restaurants”);
$sth->execute();
my $gen = XML::Generator->new(‘escape’ => ‘always’,
‘pretty’ => 2);
while(my $row = $sth->fetchrow_hashref())
{
my $xml
= $gen->restaurant($gen->name($row->{name}),
$gen->points({“longitude” => $row->{lng},
latitude => $row->{lat}
}));
print $xml,”\n”;
}
$dbh->disconnect();
Listing 5-30: XML Generated from Your SQL Database