1

I am trying to execute the following code, but it gives the error code :

use XML::Simple;
use Data::Dumper;

$xml = new XML::Simple (KeyAttr=>[]);# read XML file

$error =$xml->XMLin("trial.xml");
print "There are " . scalar@{$error->{problem}} . " problems.\n";

so it gives error prescribed at line :

print "There are " . scalar@{$error->{problem}} . " problems.\n";

Please let me know what I am doing wrong. thank you.

2
  • Try print Dumper($error); after $error =$xml->XMLin("trial.xml");, seems, that the XML file is not read properly. Commented May 23, 2013 at 9:47
  • What is the XML input? Commented May 23, 2013 at 9:47

2 Answers 2

3

As per the error message, $error->{problem} is not an array reference. The usual cause for this is that there is only one problem under error, as opposed to several, in which case XML::Simple doesn't generate an array.

Look for the ForceArray option in the docs: https://metacpan.org/module/GRANTM/XML-Simple-2.20/lib/XML/Simple.pm#ForceArray-1-in

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at the documentation:

ERROR HANDLING

The XML standard is very clear on the issue of non-compliant documents. An error in parsing any single element (for example a missing end tag) must cause the whole document to be rejected. XML::Simple will die with an appropriate message if it encounters a parsing error.

If dying is not appropriate for your application, you should arrange to call XMLin() in an eval block and look for errors in $@. eg:

my $config = eval { XMLin() };
PopUpMessage($@) if($@);

regards, Matthias

1 Comment

@ikegami, I had a brain lag thinking the questioner wanted to catch XML errors, as the variable is called $error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.