160 Part III — Google Map Hacks
Listing 9-8(continued)
push(@lines,
sprintf(‘<marker lat=”%f” lng=”%f” title=”%s”/>’,
$row->{lat},
$row->{lng},
$row->{title}));
}
$sth->finish();
if (scalar @lines > 0)
{
print(“<marker>\n”,
join(“\n”,@lines),
“</marker>\n”);
}
The script in Listing 9-8 can also be adapted into Listing 9-9 so that the information is gener-
ated dynamically, through a CGI, into the XML required by the HTML interface. The only
difference is the addition of the correct HTTP header type.
Listing 9-9:Generating the XML from a Database through CGI
#!/usr/bin/perl
use DBI;
use strict;
use CGI qw/:standard/;
print header(-type => ‘text/xml’);
my $dbh = DBI->connect( ‘dbi:mysql:database=mapsbookex;host=db.maps.mcslp.com’,
‘mapsbookex’,
‘examples’,
);
if (!defined($dbh))
{
die “Couldn’t open connection to database\n”;
}
print(“<marker>\n”);
my $sth = $dbh->prepare(‘select title,lat,lng from ch09_simple’);
$sth->execute();
while (my $row = $sth->fetchrow_hashref())
{