Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Get errors and line no's while parsing XML file

by kalyanrajsista (Scribe)
on May 20, 2010 at 12:44 UTC ( #840913=perlquestion: print w/ replies, xml ) Need Help??
kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

Hello all

I'm validating one XML using XML::LibXML module using XSD and if there are any errors in forming the XML it is returning all the errors.

Problem here is that If I'm trying to validate a huge file say 50MB, its very hard for me to find where exactly is the problem. Is there any way that I can show the user the error along with line number in the message using any Perl Module

Many Thanks

use strict; use XML::LibXML; my $parser = XML::LibXML->new; my $doc; my $RAW_FILE = 'sample.xml'; eval { $doc = $parser->parse_file($RAW_FILE); };

Comment on Get errors and line no's while parsing XML file
Download Code
Re: Get errors and line no's while parsing XML file
by Krambambuli (Deacon) on May 20, 2010 at 13:01 UTC
    Maybe it would suffice to just print out $. ($INPUT_LINE_NUMBER) together with the error code?


    Krambambuli
    ---
Re: Get errors and line no's while parsing XML file
by almut (Canon) on May 20, 2010 at 13:03 UTC

    What do you get if you inspect $@ in case an error is thrown?  (IIRC, the error messages do include line number information.)

    Update: for example, when I add the line

    print $@ if $@;

    to your code snippet, and with a sample.xml containing

    <?xml version="1.0" encoding="UTF-8"?> <mydoc> <foo> <bar>... </bar_> </foo> </mydoc>

    I do get:

    $ ./840913.pl sample.xml:5: parser error : expected '>' </bar_> ^ at ./840913.pl line 11

      I didn't notice that error line does consists of error line no.

      When I print $@, it is returning only the first error it encountered. Any other way to get all consolidated errors

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://840913]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (18)
As of 2014-07-30 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (239 votes), past polls