解析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