Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
Replies are listed 'Best First'.
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:

    $ ./ sample.xml:5: parser error : expected '>' </bar_> ^ at ./ 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

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?


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2015-10-13 09:03 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (297 votes), past polls