Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: XML::Simple giving a non-specific error

by ikegami (Patriarch)
on Mar 12, 2010 at 00:00 UTC ( [id://828162]=note: print w/replies, xml ) Need Help??


in reply to XML::Simple giving a non-specific error

You could edit the module to display which tag was mismatched. The error probably occurred at the last occurrence of that tag before the given line. You could maybe add some meta data to provide an even clearer message.

Update: Well, it appears it's not simple to update the message as it originates in expat, an external library. But that means you'll get a different message if you use a different backend for XML::Simple. For example, if you change

$XML::Simple::PREFERRED_PARSER = 'XML::Parser';
to
$XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX';
You get
Entity: line 12: parser error : Opening and ending tag mismatch: ERROR line 8 and ROOT
</ROOT>
       ^
Entity: line 13: parser error : Premature end of data in tag ERROR line 8

^
Entity: line 13: parser error : Premature end of data in tag ROOT line 3

^

My test shows that XML::Parser is the fastest backend for XML::Simple, but you can switch to a slower parser that gives better error messages for debugging.

Replies are listed 'Best First'.
Re^2: XML::Simple giving a non-specific error
by liverpole (Monsignor) on Mar 12, 2010 at 00:28 UTC
    ... if you change
    $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
    to
    $XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX';

    That output certainly looks more like what I want, even though I never said my code contained:

    $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
    I've just installed XML::SAX with ppm, but the addition of this line:
    $XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX';
    throws the error:
    Can't locate object method "new" via package "XML::SAX" at C:/Perl/sit +e/lib/XML/ SAX/ParserFactory.pm line 41, <DATA> line 13.

    But thanks, that does give me an alternative that I can do more research on.   And as I said, the XML is really basic (and reasonably short), so I'm quite certain speed won't be an issue.

    Update:  On a hunch I tried "ppm install XML::LibXML::SAX", and that got me past the "Cannot locate object" error above.  Now I'm getting a much nicer error message:

    Error while parsing XML: Opening and ending tag mismatch: ERROR line 8 and ROOT Premature end of data in tag ERROR line 8 Premature end of data in tag ROOT line 3 at C:/Perl/site/lib/XML/LibXM +L/SAX.pm l ine 64 at C:/Perl/lib/XML/Simple.pm line 370

    So ++thanks again; it appears to fit my needs nicely.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      I never said my code contained $XML::Simple::PREFERRED_PARSER = 'XML::Parser';

      I realise it wasn't there explicitly, but by virtue of not setting it explicitly and having XML::Parser installed, it's as if you had.

      Can't locate object method "new" via package "XML::SAX

      Hum, weird. Did you also install XML-LibXML to provide XML::LibXML::SAX? It would be a bad failure mode if not having XML::LibXML::SAX is the cause of that error, but you never know.

      ( I see from your update that this was indeed the problem )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://828162]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found