I'm trying to extract a small amount of data from an XML file into a csv file using perl and XML::Simple.
Here is an edited version of the data:
<?xml version="1.0" encoding="UTF-8"?>
<orders xmlns="http://www.demandware.com/xml/impex/order/2006-10-31">
<order order-no="W100148941">
<order-date>2011-08-22T16:15:47.000Z</order-date>
<custom-attributes>
<custom-attribute attribute-id="basket_notes">bnotes974211</custom-attribute>
<custom-attribute attribute-id="omOrderID">974211</custom-attribute>
</custom-attributes>
</order>
</orders>
using this script:
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple;
$data = $xml->XMLin("$ARGV[0]", ForceArray=>1);
print Dumper($data);
foreach $o (@{$data->{order}}) {
print "$ARGV[1]", ",";
print "$ARGV[2]", ",";
print "$ARGV[3]", ",";
print "$ARGV[4]", ",";
print $o->{"order-no"}, ",";
print $o->{"order-date"}, ",";
foreach my $o ( @{ $data->{'custom-attribute'} } ) {
print 'in level 1';
foreach my $attr ( @{ $data->{'custom-attribute'} } ) {
print 'in level 2';
if ( $attr->{'attribute-id'} eq 'basket_notes' ) {
print '"', $data->{'content'}, '"', ",";
}
}
}
print "\n";
}
gets me this output:
,,,,W100148941,ARRAY(0x7f7f63a524c0),
Not using the ForceArray option XMLin will replace the ARRAY(...) above with the correct value, but won't work with files with only one data element, and, as is evident, this code never does make into the custom attribute array to print anything.
What am I doing wrong?
update:
changing the looping code in the above to this:
foreach $o (@{$data->{order}})
{
print "$ARGV[1]", ",";
print "$ARGV[2]", ",";
print "$ARGV[3]", ",";
print "$ARGV[4]", ",";
print $o->{"order-no"}, ",";
#print $o->{"order-date"}, ",";
print $o->{"order-date"}->[0], ",";
foreach my $o ( @{ $data->{'custom-attributes'} } ) {
print 'in level 1';
foreach my $attr ( @{ $o->{'custom-attribute'} } ) {
print 'in level 2';
if ( $attr->{'attribute-id'} eq 'omOrderID' ) {
print '"', $data->{'content'}, '"', ",";
}
}
}
print "\n";
}
yields this:
,,,,W100148941,2011-08-22T16:15:47.000Z,
It would appear that the code is just not getting into the custom-attributes loop, and I don't know why.