解析XML時我做錯了什麼。 不知何故,我必須迭代兩個循環,其中只需要一個循環。我在這裏做錯了什麼?perl,使用XML解析XML ::簡單
首先XML數據:
<fishy>
<fish displayName = "Scalar"
description = "the first fish I bought."
/>
<fish displayName = "crystal red"
description = "not really a fish."
/>
</fishy>
然後我的Perl代碼:
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
#Read configuration
my $simple = XML::Simple->new();
my $data = $simple->XMLin('test.xml');
print Dumper($data) . "\n";
my @fishes = $data->{fish};
#This is weird.. Why do we have nested arrays?
foreach my $fish (@fishes)
{
foreach my $innerFish (@{$fish})
{
print ("\n" . $innerFish->{displayName} . " is " . $innerFish->{description});
}
print ("\n");
}
最後的反應(這是罰款)
$VAR1 = {
'fish' => [
{
'description' => 'the first fish I bought.',
'displayName' => 'Scalar'
},
{
'description' => 'not really a fish.',
'displayName' => 'crystal red'
}
]
};
Scalar is the first fish I bought.
crystal red is not really a fish.
如果沒有,會發生什麼? – 2013-02-26 08:07:35