Bad XML Causes XML::Simple to exit
Bad XML Causes XML::Simple to exit
am 05.01.2006 14:30:24 von Ben Holness
Hi all,
I am using XML::Simple to parse a basic XML document.
If, however, the XML document is not well formed, the script terminates
with a mismatched tag error.
I would prefer to handle the error myself. I have searched on the web, but
it seems that XML::Simple only has XMLin and XMLout functions. Is there
any way to change this behaviour using XML::Simple?
Cheers,
Ben
Re: Bad XML Causes XML::Simple to exit
am 05.01.2006 18:08:23 von chris-usenet
Ben Holness wrote:
> If, however, the XML document is not well formed, the script terminates
> with a mismatched tag error.
Yes, as per XML parsing requirements. (At least, that's how I understand
it.)
> I would prefer to handle the error myself. I have searched on the web, but
> it seems that XML::Simple only has XMLin and XMLout functions. Is there
> any way to change this behaviour using XML::Simple?
my $x = new XML::Simple;
my $r = eval { $x->XMLin ('somefile.xml') };
if ($@) { warn "Error: $@\n"; }
Chris
Re: Bad XML Causes XML::Simple to exit
am 05.01.2006 21:03:57 von Ben Holness
On Thu, 05 Jan 2006 17:08:23 +0000, chris-usenet wrote:
> Ben Holness wrote:
>> If, however, the XML document is not well formed, the script terminates
>> with a mismatched tag error.
>
> Yes, as per XML parsing requirements. (At least, that's how I understand
> it.)
Really? XML parsing requires a script to *exit* if the XML is malformed???
I understand that it might abort the parsing, but surprised that it has to
terminate the whole script!
> my $x = new XML::Simple;
> my $r = eval { $x->XMLin ('somefile.xml') }; if ($@) { warn "Error:
> $@\n"; }
Aha! Thankyou. I haven't come across eval before :)
Cheers,
Ben
Re: Bad XML Causes XML::Simple to exit
am 05.01.2006 21:10:20 von Paul Lalli
Ben Holness wrote:
> On Thu, 05 Jan 2006 17:08:23 +0000, chris-usenet wrote:
>
> > my $x = new XML::Simple;
> > my $r = eval { $x->XMLin ('somefile.xml') }; if ($@) { warn "Error:
> > $@\n"; }
>
> Aha! Thankyou. I haven't come across eval before :)
Next time, then, you may wish to look at the documentation for the
module you're using:
http://search.cpan.org/~grantm/XML-Simple-2.14/lib/XML/Simpl e.pm#ERROR_HANDLING
which contains this exact solution.
Paul Lalli
Re: Bad XML Causes XML::Simple to exit
am 06.01.2006 14:55:10 von chris-usenet
Paul Lalli wrote:
> Next time, then, you may wish to look at the documentation for the
> module you're using [...] which contains this exact solution.
From my perspective, using eval was so much second nature that I didn't
even consider checking the documentation for a solution to give to the OP.
I guess I've been doing too much XML parsing recently... ;-)
Chris