76 Part I — Basics
Listing 5-11 (continued)
‘China Inn’ => {longitude => -0.6391666,
latitude => 52.911111},
};
foreach my $name (sort keys %{$restaurants})
{
my $xml
= $gen->restaurant($gen->name($name),
$gen->points(
{“longitude” => $restaurants
->{$name}
->{longitude},
latitude => $restaurants
->{$name}
->{latitude}
}));
print $xml,”\n”;
}
As you can see, to the XML::Generatorobject, function names become the tag name and
the argument becomes tag data (or subtags). Attributes for a given tag can be inserted by sup-
plying a hash reference; keys are used as the attribute name and the values are the attribute val-
ues. Listing 5-12 shows the generated output.
Listing 5-12: XML Generated through the XML::Generator Module
<restaurant>
<name>China Inn</name>
<points longitude=”-0.6391666” latitude=”52.911111” />
</restaurant>
<restaurant>
<name>One on Wharf</name>
<points longitude=”-0.64” latitude=”52.909444” />
</restaurant>
You won’t always, however, be generating the information directly from other sources like the
fixed-width record file or a database, as demonstrated here. Sometimes the information will be
in XML format already (for example, as an export from another application), and that data will
need to be parsed, extracted, and reformatted, either for importing into a database or an alter-
native XML format.