nevafuse has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to perl. I'm parsing an XML string and need to print out its context. I'm having a really tough time with the correct syntax to use to access the array/hash it creates.

Data dumper prints out this...

$VAR1 = [
'ResultSet',
[
{
'xmlns' => 'urn:yahoo:maps',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation' => 'urn:yahoo:maps http://api.local.yahoo.com /MapsService/V1/GeocodeResponse.xsd'
},
'Result',
[
{
'precision' => 'address'
},
'Latitude', {}, 0, '28.312216' ,
'Longitude', {}, 0, '-81.380514' ,
'Address', {}, 0, '2335 Pepper Tree Ct' ,
'City', {}, 0, 'Kissimmee' ,
'State', {}, 0, 'FL' ,
'Zip', {}, 0, '34744-2820' ,
'Country', {}, 0, 'US'
]
]
];

How do I access the latitude and longitude?

I tried this...

$data = $xml->parse($thedata);
print $data->{ResultSet}->{Result}->{Latitude};

But no luck. Any help would be greatly appreciated. Sorry the formatting was slightly destroyed. Also, I have to use XML::Parser, was using XML::Simple but encountered problems.

Replies are listed 'Best First'.
Re: XML::Parser reader
by toolic (Bishop) on Mar 06, 2009 at 18:08 UTC
    Since you are new to Perl, and therefore have no allegiance to any particular XML parser module, I strongly recommend trying XML::Twig. Like you, I have tried using XML::Parser, but I also had "a really tough time with the correct syntax". Then I discovered XML::Twig.

    If you update your original post with a small sample of your actual XML file, rather than the hard-to-read Dumper output, I will (try) to give you a simple Perl code sample to parse what you want. Enclose the XML in "code" tags, according to Writeup Formatting Tips

      I can probably figure out how to do it in Twig. I'll try it and try to compile it. Hopefully it will work better than XML::Simple. Thanks.
Re: XML::Parser reader
by shmem (Chancellor) on Mar 06, 2009 at 20:03 UTC

    For the structure you posted, Latitude seems to be

    $data = $xml->parse($thedata); print $data->[1]->[2]->[2]->[2],"\n";

    - it seems to be, for your data is obfuscated, since you didn't enclose it between <code></code> tags as per Writeup Formatting Tips.

Re: XML::Parser reader
by eff_i_g (Curate) on Mar 06, 2009 at 18:03 UTC
    XML::Parser is a little too raw for my taste.
    What problems did you have with XML::Simple?
    Have you tried XML::Twig?
    What else will you be doing with this document?
      The problems I had with XML::Simple is I was using par to compile it into an exe, and it isn't compiling correctly. So I had to find another way to either parse a web address or parse a string. And XML::Parser seems to be the only other one I've found so far that will parse a string. I just need to read the data, nothing too complex.
Re: XML::Parser reader
by codeacrobat (Chaplain) on Mar 06, 2009 at 22:24 UTC
    General advice. Try Devel::REPL. You can take a step by step approach to get the correct statement to access data in a deeply nested datastructure. If you use Devel::REPL, make sure you also load the plugins (easiest is to set them in the default config).
    cat $HOME/.re.pl/repl.rc $_REPL->load_plugin($_) for qw(History LexEnv MultiLine::PPI DDS);

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});