开发者

unable to print correct hash key value using xml simple module

I've been testing use of XML::Simple with perl. I am able to print out some of the data, but cannot print out the file name and byte size in my sample. Can someone show me how to extract the following information from this xml file?

I would like to get:

  • file directory: /storage/foobar/test/queues/20110731
  • file name: myfilename-00
  • file size: 1234567891

So far I can get the file directory, but file name is giving me a hash value and getting file size isn't working.

Here is code so far:

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";
}

Result:

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: 

Dumper:

sample 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>


The file record will correspond to eahc <file...> tag in your XML. So you effectively need some kind of loop. Remember, $data is effectively pointing at your <listing...> tag

I have not tested it, but this is the gist of what you want

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";
}

Edit: I'm a little confused by your output. With forcearray => 1 I would have expected $data->{file} to be an arrayref, not a hashref


Apparently, and hardly unexpectedly, $data->{file} is a reference to a structure containing all the attributes of the file. Try $data->{file}->{path} for the filename, and ->{size} for the size.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜