Chapter 5 — Storing and Sharing Information 79
First, parse the supplied file into an XML object
my $doc = $parser->parsefile ($ARGV[0]) or die “Couldn’t parse $ARGV[0]”;
Extract a list of XML fragments enclosed by the tag
my $nodes = $doc->getElementsByTagName (“points”);
Get the number of objects returned
my $n = $nodes->getLength;
Now process through the list of tags and extract the data
for (my $i = 0; $i < $n; $i++)
{
Get a single tag structure from the XML document
my $node = $nodes->item ($i);
Extract the value from XXX
my $long = $node->getAttributeNode(“longitude”)->getValue();
Extract the value from XXX
my $lat = $node->getAttributeNode(“latitude”)->getValue();
print “Longitude: $long, Latitude: $lat\n”;
}
Free up the structure
$doc->dispose;
These are the key elements in the script:
1.The XML document is loaded and parsed into a DOM object.
2.A list of pointselements is extracted from the main document using the
getElementsByTagName()function.
3.A loop is used to iterate through the list of pointselements.
4.Each pointselement is extracted.
5.The values of the longitudeand latitudeattributes are obtained using the
getAttributeNode()function on the pointselement object.
Parsing XML with JavaScript
Once you go beyond the statically created Google Maps examples and start working with more
complicated sets of data, you must use XML as a method for exchanging information between
your web server application and the Google Maps client-side application. All of this is
achieved through JavaScript.
JavaScript, in its base form, is a fairly limited language designed with some strict rules and
goals in mind. Although it doesn’t contain a specific interface for working with XML docu-
ments, JavaScript can use the Document Object Model (DOM) to work with XML (and
HTML) documents. Technically, the DOM is not part of JavaScript, but JavaScript does