Code:
#!/usr/bin/perl -w
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $xml=new XML::Simple;
my $doc=$xml->XMLin(&get_str(),ForceArray => ['internal'],KeyAttr => 'id');
print Dumper($doc);
sub get_str {
return qq#<?xml version="1.0"?>
<!DOCTYPE test SYSTEM "http://www.kegg.jp/kegg/xml/KGML_v0.7.1_.dtd">
<test name="A" >
<node id="11" name="test1" >
<internal id="111" name="A111"/>
</node>
<node id="12" name="B">
<internal id="121" name="B121"/>
<internal id="122" name="B122"/>
</node>
</test>#;
}
Output:
$VAR1 = {
'name' => 'A',
'node' => {
'11' => {
'name' => 'test1',
'internal' => {
'111' => {
'name' => 'A111'
}
}
},
'12' => {
'name' => 'B',
'internal' => {
'122' => {
'name' => 'B122'
},
'121' => {
'name' => 'B121'
}
}
}
}
};
Reason:
Note 1: The default value for 'KeyAttr' is ['name', 'key', 'id']. If you do not want folding on input or unfolding on output you must set this option to an empty list to disable the feature.
Note 2: If you wish to use this option, you should also enable the ForceArray option. Without 'ForceArray', a single nested element will be rolled up into a scalar rather than an array and therefore will not be folded (since only arrays get folded).