XML-SAX 错误:不是 ARRAY 引用

发布于 2024-10-01 07:13:09 字数 1820 浏览 1 评论 0原文

(使用 WinXP Pro、ActivePerl 5.10.1 和 XML-SAX 0.96)

MyXML.xml 作为此

<?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;

输出:

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.

我不知道数组引用是什么意思? Dumper() 运行良好,但无法将数据打印到 TXT 文件。

实际上,示例代码几天前运行得很好,然后我记得我从 V5 升级了我的 Komodo Edit。到最新的V6。

今天,我只是尝试改进脚本,在开始阶段,我修复了另一个错误。在谷歌帮助下“找不到 ParserDetails.ini”。 (我之前没有收到错误!)

但现在我收到 ARRAY 引用错误。我刚才已经通过 PPM 重新安装了 XML-SAX。它仍然不起作用。

(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 the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-10-08 07:13:09

您设置的整个 XML 解析堆栈工作正常,如解析树的转储所示。 XML::SAX 不是问题的原因,它只是间接涉及。

该错误只是由于对 XML::Simple 生成的数据结构的访问不当所致。

我能猜到发生了什么。在程序的早期版本中,您启用了 ForceArray 选项(这是一个很好的做法,请参阅 选项STRICT_MODE ,并且编写遍历解析树的算法也考虑到了这一点,即涉及数组访问。

在您的程序的当前版本中,未启用ForceArray,但遍历算法不再与数据结构匹配。我建议重新启用文档中推荐的选项。

#!/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'
    }
}

这棵树现在看起来像这样:

{
    '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']
                }
            ]
        }
    ]
}

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']
                }
            ]
        }
    ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文