RE: Not an ARRAY reference. Problems reading a simple XML file
- From: Jenda@xxxxxxxxxxx (Jenda Krynicky)
- Date: Mon, 02 Apr 2007 16:34:05 +0200
From: Andrew Curry <andrew.curry@xxxxxxxxxxxx>
If you look at $data for the problem file its not an array at that point.
For a single element XML::Simple doesn't display your data in a list i.e.
With 1 element of record.
$VAR1 = {
'RECORD' => {
'COLLNAME' => 'FBIS2004',
'DOCI' => 'CP1'
}
};
With 2 elements of record.
$VAR1 = {
'RECORD' => [
{
'COLLNAME' => 'FBIS2004',
'DOCI' => 'CP1'
},
' BOOB'
]
};
So you cant do @{$data->{RECORD}} as it doesn't exist when there is only 1
element.
You can either test that its an arrayref or hashref or you could use
something like XML::Parser, Lib:XML
Or you can tell XML::Simple that you expect the <RECORD> to be
repeated and therefore want it to be an array always. Have a look at
the forcearray option.
And if your XML starts to get too big to fit in memory have a look at
XML::Rules.
my $parser = XML::Rules->new(
rules => [
_default => 'content',
RECORD => sub {
do whatever you need with the record
the data are in $_[1]->{COLLNAME} and $_[1]->{DOCI}
return; # don't remember the data for the RECORD anymore
},
DATA => '',
]
);
$parser->parsefile('test.xml');
Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
.
- Follow-Ups:
- Project Management
- From: Nigel Peck
- Project Management
- References:
- RE: Not an ARRAY reference. Problems reading a simple XML file
- From: Andrew Curry
- RE: Not an ARRAY reference. Problems reading a simple XML file
- Prev by Date: Re: Not an ARRAY reference. Problems reading a simple XML file
- Next by Date: Re: extracting an email address from a string
- Previous by thread: RE: Not an ARRAY reference. Problems reading a simple XML file
- Next by thread: Project Management
- Index(es):
Relevant Pages
|