http://www.perlmonks.org?node_id=916626

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

Is it possible to stop parsing once you find what you are looking for in a big XML file? My code looks like this:
use XML::SAX::ExpatXS; use WDSTreeHandler; ... my $tree = read_file("/x/y/z/aap.xml"); my $handler = WDSTreeHandler->new; my $parser = XML::SAX::ExpatXS->new(Handler => $handler); $parser->parse_string($tree);
With the WDSTreeHandler looking like so:
package WDSTreeHandler; use base qw(XML::SAX::Base); ... sub start_document { my ($self, $doc) = @_; } sub start_element { my ($self, $el) = @_; ... if ($level >= 0 && $el->{'LocalName'} =~ /folder|leaf/) { ## do some stuff and try to STOP PARSING } } sub end_element { ... } sub end_document { } 1;
How do you stop parsing?

Replies are listed 'Best First'.
Re: XML::SAX::ExpatXS - stop parsing?
by ikegami (Patriarch) on Jul 25, 2011 at 21:58 UTC
    How about
    die "DONE\n"; ... if (!eval { $parser->parse_string($tree); 1 # No exception }) { die $@ if $@ ne "DONE\n"; }
      Hmm why didn't i think of that? Is that stuff safe with mod_perl?
        die in one thread and process does not affect other threads or processes.
Re: XML::SAX::ExpatXS - stop parsing?
by grantm (Parson) on Jul 26, 2011 at 21:43 UTC
    An alternative to the SAX API is the 'Pull' parser API as implemented in XML::LibXML::Reader. This API is well suited to the pattern of stopping the parse when you've found what you want.