XML-SAX Error: Not an ARRAY reference
(WinXP Pro, ActivePerl 5.10.1, and XML-SAX 0.96 used)
MyXML.xml as this
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<subItem>
<bib>0006</bib>
<name>Stanley Cheruiyot Teimet</name>
<team>肯尼亚</team>
<time>2:17:59</time>
<rank>1</rank>
<comment />
<gender>Male</gender>
<distance>Full</distance>
<year>2010</year>
</subItem>
</DocumentElement>
MyPerl.pl
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
use utf8;
open FILE, ">myXML.txt" or die $!;
my $tree = XMLin('./myXML.xml');
print Dumper($tree);
print FILE "\n";
for (my $i = 0; $i < 1; $i++)
{
print FILE "('$tree->{subItem}->[$i]->{distance}')";
}
close FILE;
Output:
D:\learning\perl\im>mar.pl
$VAR1 = {
'subItem' => {
'distance' => 'Full',
'time' => '2:17:59',
'name' => 'Stanley Cheruiyot Teimet',
'bib' => '0006',
'comment' => {},
'team' => '肯尼亚',
'rank' => '1',
'year' => '2010',
'gender' => 'Male'
}
};
Not an ARRAY reference at D:\learning\perl\im\mar.pl line 41.
I don't know what the Array reference means? The Dumper()
works well.But can't print the data to TXT file.
Actually, the sample code ran well days before, Then I remember I upgrade my Komodo Edit from V5. to newest V6.
Today, I just try to improve t开发者_如何学JAVAhe script, at the beginning stage, I fixed another error. "Could not find ParserDetails.ini" with google help. (I didn't get the error before!)
But now I get the ARRAY reference error. I already reinstalled my XML-SAX via PPM just now. It still doesn't work.
The whole stack for XML parsing that you have set up works fine, as the dump of the parsed tree shows. XML::SAX is not the cause of the problem, and it is only involved indirectly.
The error comes simply from improper access to the data structure which was generated by XML::Simple.
I can guess what happened. In an earlier version of your program, you had the ForceArray
option enabled (that is a good practice, see OPTIONS and STRICT_MODE in XML::Simple), and the algorithm for traversing the parsed tree also was written to take this into account, i.e. there is array access involved.
In the current version of your program ForceArray
is not enabled, but the traversing algorithm does not match the data structure any more. I suggest to re-enable options that are recommended in the documentation.
#!/usr/bin/env perl
use utf8;
use strict;
use warnings FATAL => 'all';
use IO::File qw();
use XML::Simple qw(:strict);
use autodie qw(:all);
my $xs = XML::Simple->new(ForceArray => 1, KeyAttr => {}, KeepRoot => 1);
my $tree = $xs->parse_file('./myXML.xml');
{
open my $out, '>', 'myXML.txt';
$out->say;
for my $subitem (@{ $tree->{DocumentElement}->[0]->{subItem} }) {
$out->say($subitem->{distance}->[0]); # 'Full'
}
}
The tree looks like this now:
{
'DocumentElement' => [
{
'subItem' => [
{
'distance' => ['Full'],
'time' => ['2:17:59'],
'name' => ['Stanley Cheruiyot Teimet'],
'bib' => ['0006'],
'comment' => [{}],
'team' => ["\x{80af}\x{5c3c}\x{4e9a}"],
'rank' => ['1'],
'year' => ['2010'],
'gender' => ['Male']
}
]
}
]
}
精彩评论