in reply to Re: Using KML in XML::Simple
in thread Using KML in XML::Simple

Thanks monks.

I thought is best to start simple and build up.
So firstly, before getting onto attributes and order let's just try to get the parser loading in KML!

I am using this <kml> (thanks Fletch):

<?xml version='1.0' encoding='UTF-8'?><kml xmlns='http://earth.google. +com/kml/2.0'> <Document> <open>0</open> <name>Gazetteer for Scotland: Settlements</name> <Placemark> <name><![CDATA[ Hermits and Termits ]]></name> <!-- <Snippet> </Snippet> --> <styleUrl>#SGStyle</styleUrl> <description><![CDATA[A small harled five-bay mansion lying gable- +on to St Leonard's Street in S Central Edinburgh, Hermits and Termits + was built in 1734 for William Clifton, a Solicitor of Excise. It too +k its name from the poetic-slang for a pair of crofts, <i>Hermitis</i +> and <i>Terraris</i>, which can be traced back to at least the 15th +century and were associated with the St Leonard's Almshouses. Hermits and Termits was restored in 1982, an early project by local ar +chitect Ben Tindall, to form his own residence. The interior is panel +led and the staircase features a fine mahogany balustrade. <p><a href="http://getamap.ordnancesurvey.co.uk/getamap/frames.htm?map +Action=zoomabs&mapX=326472&mapY=672772&zoomLevel=6&isGeo=y">SHOW OS M +AP</a>,</p> <p><a href="http://www.geo.ed.ac.uk/scotgaz/ features/featurefirst16994.html"//]]></description> <LookAt> <longitude>-3.17876</longitude> <latitude>55.94218</latitude> <range>2000</range> <tilt>75</tilt> <heading>0</heading> </LookAt> <Point id="g16994"> <coordinates>-3.17876,55.94218,0</coordinates> </Point> <extra xmlns:gml="http://www.opengeospatial.org/standards/gml"> <Point id="g16994" srsName="EPSG27700"> <pos>326472,672772</pos> </Point> <typea>House, Mansion or Palace</typea> <typeb>Historic Building</typeb> <typec></typec> </extra> <Link>http://www.geo.ed.ac.uk/scotgaz/features/featurefirst16994.h +tml </Link> </Placemark> <Placemark> <name><![CDATA[ Queen's Drive ]]></name> <!-- <Snippet> </Snippet> --> <styleUrl>#SGStyle</styleUrl> <description><![CDATA[A road which largely encircles the hills wit +hin Edinburgh's Holyrood Park, the Queen's Drive is known as one of t +he finest carriage drives in Europe. From its lowest point to the eas +t of Palace of Holyroodhouse, the road rises sharply onto the eastern + and southern flanks of Arthur's Seat (251m / 823 feet). The higher p +ortions give spectacular views over the city. <p><a href="http://getamap.ordnancesurvey.co.uk/getamap/frames.htm?map +Action=zoomabs&mapX=326643&mapY=672943&zoomLevel=6&isGeo=y">SHOW OS M +AP</a>,</p> <p><a href="http://www.geo.ed.ac.uk/scotgaz/ features/featurefirst8976.html"//]]></description> <LookAt> <longitude>-3.17606</longitude> <latitude>55.94367</latitude> <range>2000</range> <tilt>75</tilt> <heading>0</heading> </LookAt> <Point id="g8976"> <coordinates>-3.17606,55.94367,0</coordinates> </Point> <extra xmlns:gml="http://www.opengeospatial.org/standards/gml"> <Point id="g8976" srsName="EPSG27700"> <pos>326643,672943</pos> </Point> <typea>Road or Street</typea> <typeb></typeb> <typec></typec> </extra> <Link>http://www.geo.ed.ac.uk/scotgaz/features/featurefirst8976.ht +ml </Link> </Placemark> <Placemark> <name><![CDATA[ Queen's Hall, The ]]></name> <!-- <Snippet> </Snippet> --> <styleUrl>#SGStyle</styleUrl> <description><![CDATA[Located on the west side of Clerk Street, in + the Newington District of Edinburgh, the Queen's Hall represents the + principal venue on the south-side of the city for concerts. Built in + 1823 as the Hope Park Chapel of Ease, the building has a two-storey +villa frontage, with a tall and slender classical steeple. Latterly t +he Newington and St. Leonard's Church, it was converted into a concer +t hall in 1979, and opened by Her Majesty Queen Elizabeth II, the 850 +-seat auditorium still strongly reflects its ecclesiastical origins, +although the pulpit has been replaced by an early 19th Century chambe +r organ. Painted 18th Century panels displaying the Commandments and +the Lord's Prayer, which were brought from the former Buccleuch Paris +h Church in 1950, can be seen on the stairs. Noted particularly for its baroque and classical music concerts, the H +all is home to the world-class Scottish Chamber Orchestra, but also o +ffers a programme of jazz, folk and contemporary music, in addition t +o theatre, comedy and opera as well as acting as an important venue f +or the Edinburgh International Festival. <p><a href="http://getamap.ordnancesurvey.co.uk/getamap/frames.htm?map +Action=zoomabs&mapX=326298&mapY=672698&zoomLevel=6&isGeo=y">SHOW OS M +AP</a>,</p> <p><a href="http://www.geo.ed.ac.uk/scotgaz/ features/featurefirst7848.html"//]]></description> <LookAt> <longitude>-3.18151</longitude> <latitude>55.94126</latitude> <range>2000</range> <tilt>75</tilt> <heading>0</heading> </LookAt> <Point id="g7848"> <coordinates>-3.18151,55.94126,0</coordinates> </Point> <extra xmlns:gml="http://www.opengeospatial.org/standards/gml"> <Point id="g7848" srsName="EPSG27700"> <pos>326298,672698</pos> </Point> <typea>Entertainment Venue</typea> <typeb>Historic Building</typeb> <typec></typec> </extra> <Link>http://www.geo.ed.ac.uk/scotgaz/features/featurefirst7848.ht +ml </Link> </Placemark> <Placemark> <name><![CDATA[ St Leonard's ]]></name> <!-- <Snippet> Saint Leonard's </Snippet> --> <styleUrl>#SGStyle</styleUrl> <description><![CDATA[A small district of S Central Edinburgh, St +Leonard's lies a mile (1.5 km) south southeast of the city centre. Th +e area represented land granted to Holyrood Abbey by King David I (c. +1080 - 1153), and while once being much larger, today is constrained +between Holyrood Park Road in the south and Dumbiedykes and the Pleas +ance in the north. Once associated with brewing, St. Leonard's Statio +n was also the terminus of the Innocent Railway. St Leonard's School +(1879) is now converted into private flats. The Police Headquarters f +or Central Edinburgh ('A' Division) is located in the modern St. Leon +ard's Police Station. <p><a href="http://getamap.ordnancesurvey.co.uk/getamap/frames.htm?map +Action=zoomabs&mapX=326400&mapY=672800&zoomLevel=6&isGeo=y">SHOW OS M +AP</a>,</p> <p><a href="http://www.geo.ed.ac.uk/scotgaz/ features/featurefirst7865.html"//]]></description> <LookAt> <longitude>-3.17992</longitude> <latitude>55.94259</latitude> <range>2000</range> <tilt>75</tilt> <heading>0</heading> </LookAt> <Point id="g7865"> <coordinates>-3.17992,55.94259,0</coordinates> </Point> <extra xmlns:gml="http://www.opengeospatial.org/standards/gml"> <Point id="g7865" srsName="EPSG27700"> <pos>326400,672800</pos> </Point> <typea>Urban District</typea> <typeb></typeb> <typec></typec> </extra> <Link>http://www.geo.ed.ac.uk/scotgaz/features/featurefirst7865.ht +ml </Link> </Placemark> <ExtendedData xmlns:GforS="http://www.geo.ed.ac.uk/scotgaz/"> <GforS:Copyright> <![CDATA[ <p>All Images and Text are Copyright (c) The Gazette +er for Scotland 1995-2008 </p>]]> </GforS:Copyright> </ExtendedData> </Document> </kml>
which has been generated from a perl script here:.

The script I am using to parse the KML is:

#!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; use LWP::Simple; use XML::Parser; my $url= "http://www.geo.ed.ac.uk/scotgaz/cgi_bin/mid/cygnus/scotgaz1. +5.pl"; my $foo = get ($url) or die "I can't get the feed you want"; my $xml = XML::Simple->new( ); my $parse =$xml->XMLin($foo); while (my ($key, $val) = each %{$parse->{Placemark}}) { printf "%s is at %s\n", $key, $val->{Point}{coordinates}; }
This script works when I remove the <kml> tages from the <kml> and so I think that it is due to something with XML::Simple where the first ankle brackets are removed automatically. When I run the script in debug mode the $foo contains the URL and the $parse contains the corect hash.
The value I ask is not brought back though and I am failing to see why! After this works I will then go onto the look at forceArray (which I have had some success with in the past and the other XML::Simple problems....... one small step at a time though!

Many thanks for the helpful explanations
niall

Replies are listed 'Best First'.
Re^3: Using KML in XML::Simple
by Corion (Patriarch) on May 28, 2008 at 08:45 UTC
    The value I ask is not brought back though and I am failing to see why! After this works I will then go onto the look at forceArray (which I have had some success with in the past and the other XML::Simple problems.......

    That you don't get the multiple values back for Placemark is immediately related to you not using ForceArray. So you need to attack the "two problems" in one go.

    A Perl hash can only store one value for a single key. You have multiple Placemark tags, so these cannot be stored in a single hash entry except as an array (reference). Hence, you will need (untested):

    my @places = @{$parse->{Placemark}}; while (my ($key, $val) = each %{$place}) { printf "%s is at %s\n", $key, $val->{Point}{coordinates}; }

    That code won't work for a file with only a single Placemark entry, hence you will need ForceArray.

    See References Quick Reference.