Re: Not an ARRAY reference. Problems reading a simple XML file
- From: igor.sutton@xxxxxxxxx (Igor Sutton Lopes)
- Date: Mon, 2 Apr 2007 15:29:19 +0100
On 2007/04/02, at 15:14, Dave Adams wrote:
My script is having problems reading and XML file with only one record in
it. When I add two or more records, there are no problems. The error is
"Not and ARRAY reference.."
Here is the PROBLEM xml file (test.xml):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<DATA>
<RECORD>
<COLLNAME>FBIS2004</COLLNAME>
<DOCI>CP1</DOCI>
</RECORD>
</DATA>
Here is the WORKING xml file:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<DATA>
<RECORD>
<COLLNAME>FBIS2004</COLLNAME>
<DOCI>CP1</DOCI>
</RECORD>
<RECORD>
<COLLNAME>FBIS2005</COLLNAME>
<DOCI>CP2</DOCI>
</RECORD>
</DATA>
Here is my script:
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple (KeyAttr=>[]);
$data = $xml->XMLin("test.xml");
#print Dumper($data);
print "There are " . scalar(@{$data->{RECORD}}) . " records.\n";
foreach my $var (@{$data->{RECORD}})
{
print $var->{DOCI} . "\n";
print $var->{COLLNAME} . "\n";
}
Does anyone have an explaination for this?
That occurs because $var->{$RECORD} is a hashref instead of arrayref. The code below should do what you want.
<code>
$xml = new XML::Simple( KeyAttr => [], ForceArray => [qw/RECORD/] );
</code>
--
Igor Sutton
igor.sutton@xxxxxxxxx
Attachment:
PGP.sig
Description: This is a digitally signed message part
- References:
- Not an ARRAY reference. Problems reading a simple XML file
- From: Dave Adams
- Not an ARRAY reference. Problems reading a simple XML file
- Prev by Date: Re: portability?
- Next by Date: RE: Not an ARRAY reference. Problems reading a simple XML file
- Previous by thread: Not an ARRAY reference. Problems reading a simple XML file
- Next by thread: RE: Not an ARRAY reference. Problems reading a simple XML file
- Index(es):
Relevant Pages
|