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




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



Relevant Pages

  • RE: writing to a specific point in an array file
    ... Hi Melanieab, ... would be in an XML file rather than a text file and trying to manipulate ... basically writing an empty string over the current row. ... > array file, but I wasn't sure how to replace just one item. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to dynamically resize an array?
    ... In the previous version of the program I just gave the array a large value eg. 40 as it is unlikely any season of a TV series will have that many episodes. ... The only other way would be to read the xml file twice, firstly counting the number of episodes and then declaring the array, and then read it again and fetch in the episode data. ... XPATH is specifically designed to search for the "DOM" of the XML documentt to returns a list of elements by providing it a query statement. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ArgumentException thrown when converting a memorystream to image
    ... the following code will read the contents from a> file and place them into a byte array. ... >> As I read the xml file in .NET, I parse my xml, retrieve this string, and>> try to convert it into an Image object or Bitmap in the following code. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Not an ARRAY reference. Problems reading a simple XML file
    ... If you look at $data for the problem file its not an array at that point. ... Problems reading a simple XML file ... This e-mail may contain confidential information. ... Any opinion expressed in this e-mail is personal to the sender ...
    (perl.beginners)
  • RE: ArgumentException received when converting a byte array to bitmap
    ... thread that rotating an image helped them with a similar, but different bug. ... > My byte array is an picture in VB6 StdPicture format. ... > the array as some child element in an xml file, ... > try to convert it into an Image object or Bitmap in the following code. ...
    (microsoft.public.dotnet.framework.drawing)