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

Hi there,

I am using XML::Parser to parse a 30MB XML file. This file is record oriented and I need to find a certain record in it. My question is: how can I stop the parser when I have found the record I am looking for?

Thanks in advance!!

Arjan Huijzer

Edit by tye, title, formatting

Replies are listed 'Best First'.
Re: XML::Parser question
by mirod (Canon) on Mar 20, 2003 at 10:56 UTC

    If you want to be clean you can use the finish method on the XML::Parser::Expat object:

    finish
               Unsets all handlers (including internal ones that set context), but
               expat continues parsing to the end of the document or until it
               finds an error.  It should finish up a lot faster than with the
               handlers set.

    Generally, when using XML::Parser, it is worth reading the docs for XML::Parser::Expat, as a lot of very useful methods are only described there.

Re: XML::Parser question
by diotalevi (Canon) on Mar 20, 2003 at 10:30 UTC

    There doesn't appear to be any express support within XML::Parser for interrupting the parse. I would suggest you consider employing an exception to stop the parse. Consider the following code. Also think about using the Exception::Class to further clarify that the parse is being interrupted for valid reasons instead of depending on a text message in the $@ variable.

    eval { $p->parse( ... ) }; if ($@ =~ /^Interrupted parsing/) { # The parse was stopped early. } sub whatever_handler { if ( $found_record ) { die "Interrupted parsing"; } }
Re: How to tell XML::Parser to stop?
by busch4al (Novice) on Mar 21, 2003 at 17:21 UTC
    You could just regex the file yourself without XML::Parse. Works for me.