Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: perl xml and array issue

by dezboi (Initiate)
on Nov 18, 2010 at 22:14 UTC ( [id://872363]=note: print w/replies, xml ) Need Help??


in reply to Re^3: perl xml and array issue
in thread perl xml and array issue

Okay. I updated the perl script as follows: prod.pl:
use XML::Simple; $xml = new XML::Simple( ForceArray => [qw( product color )]); $file = $xml -> XMLin('./prod.xml', ); foreach $products (@{$file->{product}}) { print "Widget: " . $products->{title} ." \n"; print "Rating: " . $products->{rating} . "\n"; print " Cost: " . $products->{cost} . "\n"; print "Colors: " . (join ", ", @{$products->{color}})."\n\n"; }
prod.xml
<shelf> <product> <title>widget1</title> <cost>0.10</cost> <rating>B</rating> <color>red</color> <color>blue</color> <color>green</color> </product> <product> <title>widget2</title> <cost>0.25</cost> <rating>S</rating> <color>pink</color> <color>gray</color> <color>orange</color> </product> <product> <title>widget3</title> <cost>0.50</cost> <rating>S</rating> <color>white</color> </product> </shelf>
However, output with a single item for "color" remains empty:
Widget: widget1 Rating: B Cost: 0.10 Colors: red, blue, green Widget: widget2 Rating: S Cost: 0.25 Colors: pink, gray, orange Widget: widget3 Rating: S Cost: 0.50 Colors:
I don't doubt that ForceArrray works, but apparently I just can't figure out the syntax (ie where to plug it in).

Replies are listed 'Best First'.
Re^5: perl xml and array issue
by ikegami (Patriarch) on Nov 18, 2010 at 22:41 UTC

    Without changing anything at all, I get:

    Widget: widget1 Rating: B Cost: 0.10 Colors: red, blue, green Widget: widget2 Rating: S Cost: 0.25 Colors: pink, gray, orange Widget: widget3 Rating: S Cost: 0.50 Colors: white

    Maybe your version of XML::Simple is buggy? Maybe you're not running the script you think you are running? Maybe you're not using the XML file you think you are using?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found