Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: A puzzle for parsing XML with XML::Simple!

by bobf (Monsignor)
on Jan 25, 2008 at 08:34 UTC ( [id://664243]=note: print w/replies, xml ) Need Help??


in reply to A puzzle for parsing XML with XML::Simple!

ForceArray won't do it, but all is not lost. Try KeyAttr instead:

my $xml = XML::Simple->new( KeyAttr => [ 'item' ] );
$VAR1 = { 'products' => { 'item' => [ { 'count' => '1', 'name' => 'a', 'price' => '1' }, { 'count' => '1', 'name' => 'b', 'price' => '1' } ] } };

Also, don't forget that even if the parser doesn't give you exactly what you want, there is always post-processing. :-)

HTH

Replies are listed 'Best First'.
Re^2: A puzzle for parsing XML with XML::Simple!
by ww (Archbishop) on Jan 25, 2008 at 14:24 UTC
    ++ for your solution, bobf,
    but an offset for your assertion that "ForceArray won't do it"
    because olus' solution (below) using FA *ALSO* does what the OP asked (for the data given).

    Update: Careless monk has no excuse for the stricken section; replace with "olus' note -- also re 'KeyAttr' is also valuable." Duh! on me and thanks to both bobf and olus for catching this before it sat there any longer.

      I probably should have said I couldn't get ForceArray to do it in my tests, thereby leaving room for someone more clever than I to find a solution using that option.

      That said, olus' solution uses KeyAttr just as mine does, not ForceArray (olus++ for reading the docs). Am I missing something?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-24 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found