XML::Parse dies hard

M

maxwells

Can anybody tell me why the following does not work as I expect it? It
is supposed to call the die function, because of the incorrect XML it
is parsing:

#!/usr/bin/perl
use XML::parser;
$p1 = new XML::parser;
if($p1->parse('<foo id="me">Hello World/foo>')){print 'xml is ok';}
else {die 'Error found - please check your xml';}

Instead it gives:

no element found at line 1, column 29, byte 29 at /usr/lib/perl5/
vendor_perl/5.8.8/i386-linux/XML/Parser.pm line 187

If I correct the xml by inserting a "<" after Hello World it prints
out 'xml is ok'.

Any help would be appreciated!

max
 
B

Brian McCauley

Can anybody tell me why the following does not work as I expect it?

Because your expectation of the behaviour of XML::parser::parse does
not match the documented behaviour.

Consult the description of the parse method in the XML::parser
documentation and adjust your expectation accordingly. Pay particular
note to the sentence "A die call is thrown if a parse error occurs.".
 
M

maxwells

Thanks, Brian.

I got it to work thus:

use XML::parser;
my $checker = new XML::parser ( Handlers => { } );
my $stream = "<foo id=\"me\"Hello World</foo>";
eval { $checker->parse($stream); };
if ($@) { print "uh oh";
die; }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top