in reply to XML::Simple troubles

Try this one:
#!/usr/bin/perl -w use strict; use warnings; use XML::Simple; use Data::Dumper; my $Document =XMLin("foo.xml"); while (my ($key, $val) = each %{$Document->{Placemark}}){ print "Found key '$key' with value " . Dumper($val); }

(I stored the XML file locally on disc, obviously).

Replies are listed 'Best First'.
Re^2: XML::Simple troubles
by Anonymous Monk on May 26, 2008 at 08:06 UTC
    Thanks for the reply but the suggestion doesn't seem to work. When running it in UNIX I get the following response:
    Pseudo-hashes are deprecated at pmonks.pl line 9. Pseudo-hashes are deprecated at pmonks.pl line 9. Found key 'LookAt' with value $VAR1 = undef; Pseudo-hashes are deprecated at pmonks.pl line 10. Pseudo-hashes are deprecated at pmonks.pl line 10. Argument "#SGStyle" isn't numeric in each at pmonks.pl line 10. Bad index while coercing array into hash at pmonks.pl line 10.
    Am I correct in saying that % denotes a hash as opposed to @ denoting an array? Also would the code suggested return all child nodes of the placemark element? What I really want to do is return out the
    <Point> <coordinates>-3,55</coordinates> </Point>
    so that I can run them in another SQL query. Surely to do this I would have to identify the lement I desire e.g. Placemark->Point->coordinates?? I appreciate your help on this one, I am just a little unsure as to the significance of Key and Value Cheers, Niall
      Are you sure you ran exactly the same piece of code that I posted? It works for me both with perl 5.8.8 and perl 5.10.0. Which perl version did you use?
      Am I correct in saying that % denotes a hash as opposed to @ denoting an array?

      Yes. See perldsc, perlreftut, perlref and perldata for details.

      Maybe you want something along these lines:

      #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; my $Document =XMLin("foo.xml"); while (my ($key, $val) = each %{$Document->{Placemark}}){ printf "%s is at %s\n", $key, $val->{Point}{coordinates}; } __END__ Abbeyhill is at -3.17075,55.95705,0 Abbey Strand is at -3.17423,55.95277,0 Abbeymount is at -3.1723,55.95524,0
        Apologies, you were correct, the script:
        #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; use LWP::Simple; my $Document =XMLin("foo.xml"); while (my ($key, $val) = each %{$Document->{Placemark}}) { printf "%s is at %s\n", $key, $val->{Point}{coordinates}; }
        does work. I had updated incorrectly the XML file, thankyou for that.

        Now that I can get the info from an XML file stored locally I tried to get the file when stored on a web server.
        I used this script but it says that the file does not exist!
        Any clues?

        #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; use LWP::Simple; use XML::Parser; my $url= "http://xweb.geos.ed.ac.uk/~s0341330/foo.xml"; my $foo = get ($url) or die "I can't get the feed you want"; my $parser = XML::Simple->new( ); my $Document =$parser->XMLin('$foo'); while (my ($key, $val) = each %{$Document->{Placemark}}) { printf "%s is at %s\n", $key, $val->{Point}{coordinates}; }

        Many thanks

        Niall

        Thanks once again but it is still not working.
        I am running perl 5.8.8 and I am still getting errors.
        The first time I ran it I had this:
        [scotgaz@fleet tut]$ perl pmonks.pl Pseudo-hashes are deprecated at pmonks.pl line 8. Pseudo-hashes are deprecated at pmonks.pl line 8. Use of uninitialized value in printf at pmonks.pl line 9. LookAt is at Pseudo-hashes are deprecated at pmonks.pl line 9. Pseudo-hashes are deprecated at pmonks.pl line 9. Argument "#SGStyle" isn't numeric in each at pmonks.pl line 9. Bad index while coercing array into hash at pmonks.pl line 9.
        and then every other time I ran it I got this error:
        [scotgaz@fleet tut]$ perl pmonks.pl Pseudo-hashes are deprecated at pmonks.pl line 8. Pseudo-hashes are deprecated at pmonks.pl line 8. Out of memory!
        I spoke to a colleague who could only suggest that any HTML encoding within the XML could cause problems although both of us think otherwise.
        The outpu you included is exactly the response I need!

        Cheers,
        Niall