Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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></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></link> <guid></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' => [ '' ], 'lastBuildDate' => [ 'Thu, 14 Jun 2012 13:00:00 + -0400' ], 'language' => [ 'en-us' ], 'ttl' => [ '120' ], 'item' => [ { 'link' => [ '' ], 'guid' => [ '' ], '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
Replies are listed 'Best First'.
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"; }

      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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2016-02-06 19:20 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (235 votes), past polls