Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use XML::TokeParser to slurp up some XML format log files and process them.

Occasionally I hit an malformed (truncated) XML file which causes XML::TokeParser to fall over with:

"no element found at line 35, column 15, byte 4948 at C:/Perl/site/lib/XML/TokePa rser.pm line 159"

The code looks something like this:

$parser = XML::TokeParser->new("somefile.xml"); if ( $parser->get_tag('footer')) { # do stuff }
It fails on the "get_tag" because the "footer" tag does not exist.

Shouldn't it do something sensible like return undef if it can't find the tag?

Any help gratefully received.

Replies are listed 'Best First'.
Re: XML::TokeParser and malformed XML
by davorg (Chancellor) on Sep 23, 2004 at 09:03 UTC

    You can use the block form of eval to catch fatal errors like that.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks - that works perfectly. Much appreciated.
Re: XML::TokeParser and malformed XML
by Anonymous Monk on Sep 23, 2004 at 10:28 UTC

    Another possiblity might be:

    while ($token = $p->get_token()) { if ($token->is_tag()) { # do something } }

    Note: I have no experience with XML::TokeParser so it is possibile that this code is completly useless...