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
filerecord will correspond to eahc<file...>tag in your XML. So you effectively need some kind of loop. Remember,$datais effectively pointing at your<listing...>tagI have not tested it, but this is the gist of what you want
Edit: I’m a little confused by your output. With
forcearray => 1I would have expected$data->{file}to be an arrayref, not a hashref