Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

78 Part I — Basics


Using this structure you can extract a list of restaurants by obtaining a list of the top-level tags
with the key ‘restaurant’. For example, the following line would print this list:
print keys %{$xml->{‘restaurant’};

The DOM method follows similar principles — you access the tags by name. The difference
is that when you access a particular tag, the return value is another XML object. The DOM
model therefore lets you walk through the document accessing different tags and different
levels of information through the document.

A number of different functions within the DOM method enable you to obtain a list of XML
tags with a particular name (for example, all of the <restaurant>tags) and also to extract
the information contained within the tags or their attributes.

For example, when parsing the document from Listing 5-12, the longitude and latitude of each
restaurant can be extracted using this method:

1.Get a list of restaurants (effectively, each restaurant is a subset of the tags in each
<restaurant>tag).
2.Get the <points>tag from each restaurant.

3.Extract the longitude and latitude attributes from this <points>tag.

Alternatively, the same information could be extracted from the XML document using this
method:

1.Get a list of the <points>tags.
2.Extract the longitude and latitude attributes from each <points>tag.

Both solutions work, but the latter is possible only because there is only one <points>tag
within each restaurant definition.

All Perl modules can be downloaded from CPAN (http://cpan.org) or through the CPAN
module. For Windows users using the ActiveState Perl distribution, use the Perl Package Manager
(PPM) or Visual Package Manager (VPM) if you have a commercial license.

The XML::DOMmodule for Perl provides an interface to the DOM methods for extracting
information from XML documents in this way. Listing 5-15 shows a sample script that
extracts the information using the second method described earlier.

Listing 5-15:Extracting Information from an XML Document Using
DOM in Perl

use XML::DOM;

my $parser = new XML::DOM::Parser;
Free download pdf