【问题标题】:not array reference with perl XML::Simple code不是 perl XML::Simple 代码的数组引用
【发布时间】:2011-10-05 03:03:47
【问题描述】:

我一直在用一些 xml 文件输出测试 XML::Simple,并注意到如果我有超过 (1) 个文件路径,我的一个 xml 文件中的哪个位置,我的脚本将工作 (2.xml强>)

如果我只有 (1) 个文件路径 (1.xml),那么我会收到以下消息:Not an ARRAY reference at ./tst-simple.pl line 28

我的问题是为什么代码不能在 (1) 文件路径上运行?我正在使用 ForceArray=>1。我该如何处理?

1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<listing time="2011-10-04T02:33:44+0000" recursive="no" path="/storage/foobar/test/queues/20110731" exclude="" filter=".*" version="0.20.202.1.1101050227">
    <directory path="/storage/foobar/test/queues/20110731" modified="2011-10-04T02:32:11+0000" accesstime="1970-01-01T00:00:00+0000" permission="drwx------" owner="unix_act" group="foobar"/>
    <file path="/storage/foobar/test/queues/20110731/myfilename-00" modified="2011-10-03T04:47:46+0000" accesstime="2011-10-03T04:47:46+0000" size="123456789" app="3" blocksize="134217728" permission="-rw-------" owner="unix_act" group="foobar"/>
</listing>

2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<listing time="2011-10-04T02:33:44+0000" recursive="no" path="/storage/foobar/test/queues/20110731" exclude="" filter=".*" version="0.20.202.1.1101050227">
    <directory path="/storage/foobar/test/queues/20110731" modified="2011-10-04T02:32:11+0000" accesstime="1970-01-01T00:00:00+0000" permission="drwx------" owner="unix_act" group="foobar"/>
    <file path="/storage/foobar/test/queues/20110731/myfilename-00" modified="2011-10-03T04:47:46+0000" accesstime="2011-10-03T04:47:46+0000" size="123456789" app="3" blocksize="134217728" permission="-rw-------" owner="unix_act" group="foobar"/>
    <file path="/storage/foobar/test/queues/20110731/myfilename-01" modified="2011-10-03T04:47:46+0000" accesstime="2011-10-03T04:47:46+0000" size="123456789" app="3" blocksize="134217728" permission="-rw-------" owner="unix_act" group="foobar"/>
</listing>

代码:

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

my $xml = $ARGV [0]; 
my $data = XMLin($xml); 

for my $xs ($xml) {
    #my $data = XMLin($xs, ForceArray => 0);
    my $data = XMLin($xs, ForceArray => 1);
    #my $data = XMLin($xs, ForceArray => [ qw ( path ) ]);
    print Dumper ($data);
}

foreach my $file( @{ $data->{file} } )
{
    my( $dir, $fname );
    if( $file->{path} =~ /^(.*)\/([^\/]+)$/ )
    {
        $dir = $1;
        $fname = $2;
    }
    else 
    {
        $dir = "";
        $fname = $file->{path};
    }
    print "This is the DIRECTORY: $dir\n"; 
    print "This is the FILE:      $fname\n";
    print "This is the FILE SIZE: $file->{size}\n";
}

【问题讨论】:

    标签: xml perl


    【解决方案1】:

    您有两个名为$data 的不同变量。一个是使用ForceArray =&gt; 1 创建的,但您使用的是未使用ForceArray =&gt; 1 创建的。

    替换

    my $data = XMLin($xml); 
    
    for my $xs ($xml) {
        #my $data = XMLin($xs, ForceArray => 0);
        my $data = XMLin($xs, ForceArray => 1);
        #my $data = XMLin($xs, ForceArray => [ qw ( path ) ]);
        print Dumper ($data);
    }
    

    my $data = XMLin($xml, ForceArray => 1);
    print(Dumper($data));
    

    【讨论】:

      猜你喜欢
      • 2013-01-28
      • 2012-06-07
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2019-08-28
      • 1970-01-01
      相关资源
      最近更新 更多