awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have some XML where it has nodes with duplicate tags and attributes. When it loads, the last duplicate value overwrites the previous one(s) in the data structure.

Is there anyway to get this to load 100% into a data structure?

I've tried all the options in XML::Simple. I'd rather not modify the XML to get rid of the duplicate values.
#!/usr/bin/perl use warnings; use strict; use XML::Simple; use Data::Dumper; my $xml = do{ local $/; <DATA> }; print Dumper XMLin($xml); __DATA__ <?xml version="1.0" encoding="UTF-8"?> <root> <test id="1"> <first id="white">Mike</first> </test> <test id="1"> <last id="black">Gifford</last> </test> </root>
See that the output doesn't have all the data... the last <test id="1"> overwrote the first.
$VAR1 = { 'test' => { '1' => { 'last' => { 'content' => 'Gifford', 'id' => 'black' } } } };

Replies are listed 'Best First'.
Re: XML::Simple - Duplicate Node Overwrites
by wind (Priest) on Apr 23, 2011 at 06:57 UTC

    Assign a different KeyAttr so that test will be treated as a list instead of a hash.

    print Dumper XMLin($xml, KeyAttr => 'foobar', );
      Yep, that did it, thanks! Changing:
      print Dumper XMLin($xml, KeyAttr => 'test');
      Changed the output:
      $VAR1 = { 'test' => [ { 'first' => { 'content' => 'Mike', 'id' => 'white' }, 'id' => '1' }, { 'last' => { 'content' => 'Gifford', 'id' => 'black' }, 'id' => '1' } ] };
        Don't let some other node accidentally get compressed into a hash instead. Set the KeyAttr to the empty string:
        print Dumper XMLin($xml, KeyAttr => '', );