Why does XML: Simple complain that the "element was not found"?

I am trying to run a simple Perl program that uses XML :: Simple to output data from an XML file. However, the error I am getting is:

no element found at line 15, column 0, byte 308 at /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/XML/Parser.pm line 185

The XML file is as follows:

<?xml version="1.0"?>
<customer-data>
<customer>
<first_name>Frank</first_name>
<last_name>Sanbeans</last_name>
<dob>3/10</dob>
<email>frank@example.com</email>
</customer>
<customer>
<first_name>Sandy</first_name>
<last_name>Sanbeans</last_name>
<dob>4/15</dob>
<email>sandy@example.com</email>
</customer>

And my perl code:

use strict;
use XML::Simple;

my $xml = XMLin('./test.xml',forcearray => 1);
foreach my $customer (@{$xml->{customer}}) {
  print "Name: $customer->{first_name}->[0] ";
  print "$customer->{last_name}->[0]\n";
  print "Birthday: $customer->{dob}->[0]\n";
  print "E-mail Address: $customer->{email}->[0]\n";
}

How can this be fixed?

Thanks.

+5
source share
2 answers

At the end of the document, you must close the client data tag at the end of the document so that it is well formed. This is why the barfs parser is at the end of the file.

+7
source

, , , ? , . : , , :

use XML::SAX;
use XML::LibXML::SAX;
$XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX';

, , XML:: SAX , XML.

0

All Articles