Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: XML::Parser - Code Reference Error?

by mifflin (Curate)
on Jun 10, 2005 at 04:55 UTC ( [id://465418]=note: print w/replies, xml ) Need Help??


in reply to XML::Parser - Code Reference Error?

I think you meant..
my $tmp = $parse->parse($address);
the $parse object needs a method
Also, the parse method seems to return a array reference, not a hash.
use strict; use warnings; use XML::Parser; use Data::Dumper; my $parse = new XML::Parser(Style => 'Objects'); my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; print Dumper $parse->parse($address);
this produces..
C:\tmp>test.pl $VAR1 = [ bless( { 'Kids' => [ bless( { 'Kids' => [] }, 'main::HNO' ), bless( { 'Kids' => [ bless( { 'Text' => + 'Eagle Way' }, 'main::C +haracters' ) ] }, 'main::STN' ), bless( { 'Kids' => [ bless( { 'Text' => + 'Gotham City' }, 'main::C +haracters' ) ] }, 'main::MCN' ) ] }, 'main::LOC' ) ];
You might want to consider XML::Simple. It has a much simpler interface
use strict; use warnings; use XML::Simple; use Data::Dumper; my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; my $ref = XMLin($address); print Dumper $ref;
This produces...
C:\tmp>test.pl $VAR1 = { 'MCN' => 'Gotham City', 'HNO' => {}, 'STN' => 'Eagle Way' };

Replies are listed 'Best First'.
Re^2: XML::Parser - Code Reference Error?
by awohld (Hermit) on Jun 10, 2005 at 05:42 UTC
    I've never worked with an array reference before.

    So how would I print the 'Text'=>'Eagle Way' from that array reference?
    So it looks like this data structure is a multidimensional array? I'm having a hard time understanding it.

    What does Data::Dumper mean by 'bless'?
      my @array = (1, 2, 7, 10); my $array_ref = \@array; print $array[2]; # prints 7 print $array_ref->[2]; # prints 7 (difference is the -> )

      bless basically means that it's not just a reference, it's a reference to an object of some class.

      Read the documentation for Data::Dumper - it's a great tool to see how these complex data structures are built.

        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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://465418]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-19 00:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found