我一直在测试使用带有perl的XML :: Simple。我能够打印出一些数据,但无法在我的样本中打印出文件名和字节大小。有人可以告诉我如何从这个xml文件中提取以下信息吗?
我想得到:
/storage/foobar/test/queues/20110731
myfilename-00
1234567891
到目前为止,我可以获取文件目录,但文件名给我一个哈希值,并且获取文件大小不起作用。
到目前为止,这是代码:
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $xml = $ARGV [0];
for my $xs ($xml) {
#my $data = XMLin($xs, ForceArray => 0);
my $data = XMLin($xs, ForceArray => 1);
#my $data = XMLin($xs, ForceArray => [ qw (directory file path ) ]);
print Dumper ($data);
print "This is the DIRECTORY: $data->{path}\n";
print "This is the FILE: $data->{file}\n";
print "This is the FILE SIZE: $data->{size}\n";
}
结果:
This is the DIRECTORY: /storage/foobar/test/queues/20110731
This is the FILE: ARRAY(0x8265c38)
Use of uninitialized value in concatenation (.) or string
This is the FILE SIZE:
自卸车:
示例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:48:04+0000" accesstime="2011-10-03T04:48:04+0000" size="987654321" app="3" blocksize="134217728" permission="-rw-------" owner="unix_act" group="foobar"/>
</listing>
答案 0 :(得分:3)
file
记录将对应于XML中的eahc <file...>
标记。所以你实际上需要某种循环。请注意,$data
实际上是指向您的<listing...>
代码
我没有测试过,但这是你想要的要点
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";
}
编辑:我对你的输出感到有些困惑。使用forcearray => 1
我希望$data->{file}
是一个arrayref,而不是hashref
答案 1 :(得分:0)
显然,并且不出意外,$data->{file}
是对包含文件所有属性的结构的引用。请尝试使用$data->{file}->{path}
作为文件名,并使用->{size}
作为文件名。