carterniall has asked for the wisdom of the Perl Monks concerning the following question:
I am having a bit of a doozy problem with parsing out values of a KML.
I have managed to get the KML into a hash and print off the values I desire although the hash is breaking my KML structure apart.
The trouble is that each attribute is being placed as a seperate hash within the node.
This will present problems when I am trying to take my value, use it to return another value and place this value back into the KML at the correct place which is what I need.
I am currently using XML::Simple and suspect that this does not recognise attributes as attributes and I am struggling to comprehend if there are better parsers which will maintain my KML/XML structure?
Another problem which I seem to be having with XML::Simple is that the <name> element of the KML is not being preserved, the value is instead being used as the Hash key value and not preserving the value as the value of the <name> element! Again, is this an XML:Simple perculiarity and is it a known problem of XML parsers?
Any suggestions/explanations would be very useful,
Many thanks
Niall Carter
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using KML in XML::Simple
by Fletch (Bishop) on May 27, 2008 at 17:44 UTC | |
Well judging by the sample code and sample XML you've helpfully provided it's easy to see where the problem is . . . Oh, never mind. At any rate, a quick perusal of the XML::Simple docs show that it certainly does handle attributes and shows the format the returned data takes. So that's probably the wrong tree up which to be barking.
The cake is a lie. | [reply] |
|
Re: Using KML in XML::Simple
by grantm (Parson) on May 28, 2008 at 02:20 UTC | |
If you're going to use XML::Simple, then please take a few minutes to read this node which shows ways to avoid common pitfalls (such as not specifying KeyAttr which is causing your 'name' problem; and not specifying ForceArray which is probably causing your other problem). Once you've read that, I'd really recommend using XML::LibXML instead - it will be easier. This node discusses moving from XML::Simple to XML::LibXML. | [reply] |
by carterniall (Novice) on May 28, 2008 at 08:37 UTC | |
I thought is best to start simple and build up. I am using this <kml> (thanks Fletch): which has been generated from a perl script here:. The script I am using to parse the KML is: 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 | [reply] [d/l] [select] |
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):
That code won't work for a file with only a single Placemark entry, hence you will need ForceArray. | [reply] [d/l] [select] |
|
Re: Using KML in XML::Simple
by psini (Deacon) on May 27, 2008 at 17:59 UTC | |
I myself don't use XML::Simple because, as every "simple" implementation, is a hell to use in not-so-simple cases... I suggest you see XMS::SAX that is a full XML parser and provides, via XML::Class::Factory the means to create your handlers to the parser events. As a serializer I am completely satisfied using XML::Writer which is simple and pratical for my needs; I suppose there are other XML serializer in CPAN, but I don't know them Update If you want to use XML::Simple, try adding ForceArray => 1, KeyAttr=>[] to the options passed to the constructor. Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man. | [reply] [d/l] |
|
Re: Using KML in XML::Simple
by moritz (Cardinal) on May 28, 2008 at 08:48 UTC | |
| [reply] |
by carterniall (Novice) on May 28, 2008 at 09:48 UTC | |
So I have had a look at the XML::Simple troubles and am now using forcearray so that when debugging and printing the contents of the $parse I have this returned: This encourages me as it appears that I now have a hash of which contains an array of Placemarks and each object in the array (of Placemark) contains a hash(?).
However I still have the problem that when I try to get the info out of the hash, nothing is returned. I can understand what is being attempted, namely that you are looking to put the array of Placemarks into an array and then loop through the array and taken values out of the hash contained within?
All help is great, thanks monks | [reply] [d/l] |