Question

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'
                                             }
                                    }
                      }
              }
    };
Was it helpful?

Solution

Add

ForceArray => [qw( node internal )]

Code:

my $doc = XMLin(\$xml,
   ForceArray => [qw( node internal )],
   KeyAttr    => [qw( id )],
);

print(Dumper($doc));

my $nodes = $doc->{node};
for my $node_key ( sort { $a <=> $b } keys %$nodes ) {
   my $node = $nodes->{$node_key};
   my $internals = $node->{internal};

   for my $internal_key ( sort { $a <=> $b } keys %$internals ) {
      print("$internal_key\n");

      # my $internal = $internals->{$internal_key};
      # print("$internal->{name}\n");
   }
}

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top