in reply to Re^2: XML::Simple troubles
in thread XML::Simple troubles

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

Replies are listed 'Best First'.
Re^4: XML::Simple troubles
by carterniall (Novice) on May 27, 2008 at 08:18 UTC
    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

      my $Document =$parser->XMLin('$foo');

      So you're trying to open a file named $foo - that won't work. Remove the single quotes.

Re^4: XML::Simple troubles
by carterniall (Novice) on May 26, 2008 at 12:17 UTC
    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

      Argument "#SGStyle" isn't numeric in each at pmonks.pl line 9.
      I find this particularly puzzling, because Line 9 doesn't try to use anything as a number, and there is no 'SGStyle' in the XML input file you pasted.

      So either your system is totally broken, or you are running the script with different code or data than I do.

      The Pseudo-hashes are deprecated suggests that the data structure is an array and you try to use it as a hash.

        My guess is that the OP is using a different data file than the one posted and that the solution likely is to use the ForceArray => [...] option as discussed in XML::Simple.