For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > XML::Parser - trapping badly formed XML ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML::Parser - trapping badly formed XML ?
Hooby

2005-07-26, 10:03 pm

I have been using the wonderful XML::Parser on a winXP box successfully for
a long time now. Unfortunately, I have several (hundred?) thousand XML files
that I process on a wly basis. Every so often - say 1 in 10,000 might be
badly formed (but which one?). When XML::Parser hits one of these it dies
(just as the documentation says it would). BTW - I have no control over the
badly formed XML if it occurs - I just have deal with what I am given...

Is there a way to trap or change this behaviour so that I can pass over the
offending file gracefully and let the program continue with the others (it
takes several hours to complete - amd I don't mind missing one file
occasionally). In other words can I trap the error in perl to make it a warn
not a die?

Any suggestions gratefully received!

Thanks Andy

Typical usage as seen below :

use XML::Parser;
$parser = new XML::Parser(Style=>'Subs', Pkg=>'SubHandlers', ErrorContext =>
2);
$parser->setHandlers( Start => \&startElement,
End => \&endElement,
Char => \&characterData
);
$currentfile = $filename;
$parser->parsefile($currentfile); # open up and parse the classification
scheme to find out what filelists were created and generate indexes as it
goes


Kevin Michael Vail

2005-07-27, 4:02 am

In article <WoBFe.7771$VG6.5489@fe07.lga>,
"Hooby" <andygillanders@xxnospamxx.charter.net> wrote:

> I have been using the wonderful XML::Parser on a winXP box successfully for
> a long time now. Unfortunately, I have several (hundred?) thousand XML files
> that I process on a wly basis. Every so often - say 1 in 10,000 might be
> badly formed (but which one?). When XML::Parser hits one of these it dies
> (just as the documentation says it would). BTW - I have no control over the
> badly formed XML if it occurs - I just have deal with what I am given...
>
> Is there a way to trap or change this behaviour so that I can pass over the
> offending file gracefully and let the program continue with the others (it
> takes several hours to complete - amd I don't mind missing one file
> occasionally). In other words can I trap the error in perl to make it a warn
> not a die?
>
> Any suggestions gratefully received!


perldoc -f eval

> Typical usage as seen below :
>
> use XML::Parser;
> $parser = new XML::Parser(Style=>'Subs', Pkg=>'SubHandlers', ErrorContext =>
> 2);
> $parser->setHandlers( Start => \&startElement,
> End => \&endElement,
> Char => \&characterData
> );
> $currentfile = $filename;
> $parser->parsefile($currentfile); # open up and parse the classification
> scheme to find out what filelists were created and generate indexes as it
> goes


eval { $parser->parsefile($currentfile); };
if ($@) {
## an error occurred
}
--
Bright eyes/burning like fire, | Kevin Michael Vail
Bright eyes/how can you close and fail? | kevin@vaildc.net
How can the light that shone so brightly | . . . . . . . . . .
Suddenly shine so pale?/Bright eyes | . . . . . . . . .
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com