我有以下 xml 文件
<?xml version="1.0"?>
<!DOCTYPE pathway SYSTEM "http://www.kegg.jp/kegg/xml/KGML_v0.7.1_.dtd">
<pathway name="path:ko01200" org="ko" >
    <entry id="1" >
        <graphics name="one" 
             type="circle" />
    </entry>
    <entry id="7" >
        <graphics name="one" 
             type="rectangle" />
        <graphics name="two" 
             type="rectangle"/>
    </entry>
</pathway>
我厌倦了使用 xml simple 和下面的代码来解析它,因为其中一个节点有 2 个图形元素,所以我被卡住了。所以它抱怨。我假设我必须有另一个用于图形元素的 foreach 循环,但我不知道如何进行。
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $xml=new XML::Simple;
my $data=$xml->XMLin("file.xml",KeyAttr => ['id']);
print Dumper($data);    
foreach my $entry (   keys %{$data->{entry}} ) {
    print $data->{entry}->{$entry}->{graphics}->{type}."\n";            
}
这是代码结果
$VAR1 = {
      'entry' => {
                 '1' => {
                        'graphics' => {
                                      'name' => 'one...',
                                      'type' => 'circle'
                                    }
                      },
                 '7' => {
                        'graphics' => [
                                      {
                                        'name' => 'one',
                                        'type' => 'rectangle'
                                      },
                                      {
                                        'name' => 'two',
                                        'type' => 'rectangle'
                                      }
                                    ]
                      }
               },
      'org' => 'ko',
      'name' => 'path:ko01200'
    };
circle
Not a HASH reference at stack.pl line 12.