Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

XML::Simple Array bombs out with 1 Item in document

by roncoe2000 (Initiate)
on Jun 14, 2012 at 18:02 UTC ( #976270=perlquestion: print w/ replies, xml ) Need Help??
roncoe2000 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Great Perl Monks. Just starting to use XML::Simple. Does a good job. Very simply , the code bombs out and doesnt print in the test foreach loop. I forced the output into a scalar array and still Bombs. This should have fixed it. Heres what in the input file.
<?xml version="1.0" encoding="ISO-8859-1" ?> <rss version="2.0"> <channel> <title>Used Cars fl</title> <link>http://usedcars.com</link> <language>en-us</language> <lastBuildDate>Thu, 14 Jun 2012 13:00:00 -0400</lastBuildDate> <copyright>Copyright 2012, West Palm Beach</copyright> <ttl>120</ttl> <item> <title><![CDATA[220 Used Cars]]></title> <pubDate>Wed, 16 May 2012 20:14:52 -0400</pubDate> <link>http://usedcars.com</link> <guid>http://usedcars.com</guid> <description>Description of used car.</description> </item> </channel> </rss> ******************************************************* Perl Code: use CGI qw/:standard/; use LWP::UserAgent; use XML::Simple; $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; use Data::Dumper; $page=new CGI; use CGI::Carp 'fatalsToBrowser'; print $page->header; $xml = new XML::Simple (KeyAttr=>[''],NSExpand => 0,ForceArray=> 1); # read XML file $html="data_records2.txt"; $data =$xml->XMLin($html); open(FILE1,">data_out.txt"); print FILE1 Dumper($data); #if ($@) { warn "Error: $@\n"; #} $config_string='{channel}{item}'; foreach $e(@{eval("\$data->$config_string")}) { $count++; print $e->{title} ,$e->{description}, $e->{link}."\n"; } close(FILE1); exit; ############################################ Heres the output . Never gets to print data in the foreach loop. $VAR1 = { 'version' => '2.0', 'channel' => [ { 'link' => [ 'http://usedcars.com' ], 'lastBuildDate' => [ 'Thu, 14 Jun 2012 13:00:00 + -0400' ], 'language' => [ 'en-us' ], 'ttl' => [ '120' ], 'item' => [ { 'link' => [ 'http://usedcars.com' ], 'guid' => [ 'http://usedcars.com' ], 'title' => [ '220 Used Cars' ], 'description' => [ 'Description of +used car.' ], 'pubDate' => [ 'Wed, 16 May 2012 20 +:14:52 -0400' ] } ], 'copyright' => [ 'Copyright 2012, West Palm Bea +ch' ], 'title' => [ 'Used Cars fl' ] } ] }; ****************************************************** Thanks you Oh mighty perl Gods--- I mean Monks. This appears too ez. I + hang my head low in light of the great ones.

Comment on XML::Simple Array bombs out with 1 Item in document
Download Code
Re: XML::Simple Array bombs out with 1 Item in document
by derby (Abbot) on Jun 14, 2012 at 18:52 UTC

    Hmm ... well ... once you get paste the dodgy use of eval (do you really need to do that ... probably not). Basically the core problem is your data structure is not a hash of hashes but a hash of arrays of hashes.

    foreach my $e ( @{$data->{channel}[0]{items}} ) { ... print $e->{title}[0] ... }
    but you're probably better of just using XML::RSS:
    my $rss = XML::RSS->new( version => '2.0' ); my $rss->parse( $rss_xml ); # print the title and link of each RSS item foreach my $item ( @{$rss->{items}} ) { print "title: $item->{title}\n"; print "link: $item->{link}\n"; print "desc: $item->{description}\n"; }

    -derby
      Thanks. But my code does work well when there are 2 or more items

        That's great. But I hear the code doesn't work well when there is only 1 item.

        And that's the case that derby is talking about. You could check to see if you've got a hash (1 item) or an array (2+ items), using ref, or see about making them always be arrays in the module's options (see ForceArray).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://976270]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-08-28 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls