RE: Not an ARRAY reference. Problems reading a simple XML file



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

.



Relevant Pages

  • RE: Create a "cell button" to hide selected cells.
    ... To avoid cluttering up the worksheet with controls, I used a UserForm. ... The user clicks a button on the worksheet to display a UserForm ... Private Sub cmdSetWeeksToShow_Click ...
    (microsoft.public.excel.worksheet.functions)
  • Re: What am I doing wrong?
    ... I do not like a single display, prefering a Flex Grid to display all of the ... Dim rstCompany As Recordset ... Set rstCompany = Nothing ... Private Sub CmdUpdate_Click ...
    (microsoft.public.vb.database.dao)
  • Re: Hardware hackers rejoice!
    ... I've tried to warm up to Perl ... sub getvar { ... # Given 0, the fan is off. ... # Display to LCD ...
    (Ubuntu)
  • Re: filter a combobox
    ... the reason for IsError is that the CB2 linked cell B3 will display ... "Dave Peterson" wrote: ... > Did you put the code behind the worksheet with the comboboxes? ... >> Private Sub ComboBox2_Change ...
    (microsoft.public.excel.programming)
  • Re: Next & Previous buttons dont display data correctly in Access
    ... The unbound controls remain ... NOTE - it is only display that is incorrect - the database ... > Exit Sub ... > The custom print button is supposed to print of a report version of the ...
    (microsoft.public.access.forms)