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); };

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 (Curate) 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
[mz2255]: I'm trying to post a question to "seekers of perl wisdom" but always getting a permission denied when I want to submit. There are no links in my post...
[ambrus]: again? someone complained about this just a few days ago (although eventually they could post)
[choroba]: Can you post the question in mz2255's scratchpad?
[ambrus]: Corion, if you're here, can you check the spam filter logs to see what's triggering this time?
[ambrus]: Petroza had trouble posting yesterday, but has posted Issues Fetching URL with a variable token since.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-10-19 15:21 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (255 votes). Check out past polls.