I want to parse the XML file that I have here and print the id
attribute of internal
elements.
This is the XML file
<?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>
and here is the code, It fails when one one node has two internal properties
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $xml=new XML::Simple;
my $doc=$xml->XMLin("test.xml",KeyAttr => ['id']);
print Dumper($doc);
foreach my $node ( sort keys %{$doc->{node}} ) {
print $doc->{node}->{$node}->{internal}->{id}."\n";
}
Here is how dumper looks like
$VAR1 = {
'name' => 'test1',
'node' => {
'11' => {
'name' => 'A',
'internal' => {
'name' => 'A111',
'id' => '111'
}
},
'12' => {
'name' => 'B',
'internal' => {
'122' => {
'name' => 'B122'
},
'121' => {
'name' => 'B121'
}
}
}
}
};