For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > January 2006 > Bad XML Causes XML::Simple to exit









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 Bad XML Causes XML::Simple to exit
Ben Holness

2006-01-10, 4:00 am

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
chris-usenet@roaima.co.uk

2006-01-10, 4:00 am

Ben Holness <usenet@bens-house.org.uk> 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
Ben Holness

2006-01-10, 4:00 am

On Thu, 05 Jan 2006 17:08:23 +0000, chris-usenet wrote:

> Ben Holness <usenet@bens-house.org.uk> wrote:
>
> 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

Paul Lalli

2006-01-10, 4:00 am

Ben Holness wrote:
> On Thu, 05 Jan 2006 17:08:23 +0000, chris-usenet wrote:
>
>
> 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-...#ERROR_HANDLING
which contains this exact solution.

Paul Lalli

chris-usenet@roaima.co.uk

2006-01-10, 4:00 am

Paul Lalli <mritty@gmail.com> 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
Sponsored Links







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

Copyright 2008 codecomments.com