in reply to Re^3: XML::Parser - Code Reference Error?
in thread XML::Parser - Code Reference Error?

So I'm trying to print from this array reference, but I'm missing something.
#!/usr/local/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use XML::Parser; use CGI; use Data::Dumper; my $q = CGI->new(); print $q->header(); my $parse = new XML::Parser(Style => 'Objects'); my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; my $tmp = $parse->parse($address); print $tmp->[0][1];
So I'm taking the array reference created by $parse->parse($address) and storing it in $tmp. Then I'm trying to print from the array reference but I get an error saying that $tmp isn't an array refernce.

What am I missing?

Replies are listed 'Best First'.
Re^5: XML::Parser - Code Reference Error?
by spurperl (Priest) on Jun 10, 2005 at 06:25 UTC
    I'm not sure that XML::Parser is what you need here, at least not for the task you're presenting. See an example of the usage of XML::Parser here.

    Perhaps all you need is XML::Simple ?

    #!/usr/local/bin/perl -w use warnings; use strict; use XML::Simple; use Data::Dumper; my $xs = new XML::Simple(KeepRoot => 1); my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; my $ref = $xs->XMLin($address); print Dumper($ref); # to print "Eagle way" print $ref->{LOC}->{STN};
Re^5: XML::Parser - Code Reference Error?
by holli (Abbot) on Jun 10, 2005 at 07:22 UTC
    In "Objects"-Style, XML::Parser returns to you a tree of objects (thus the name) that represent the XML you have parsed. That is similar to DOM, but simpler.

    Have a look at the output of
    print Dumper ($tmp);
    It's a quite complex structure: Thus, to access the values you want, you have to write:
    print $tmp->[0]->{Kids}->[1]->{Kids}->[0]->{Text}, "\n", $tmp->[0]->{Kids}->[2]->{Kids}->[0]->{Text};
    which produces
    Eagle Way Gotham City
    Or in a loop:
    for ( @{$tmp->[0]->{Kids}} ) { print $_->{Kids}->[0]->{Text}, "\n"; }


    holli, /regexed monk/