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?