Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

XML::SAX::ExpatXS - stop parsing?

by Jaap (Curate)
on Jul 25, 2011 at 21:40 UTC ( [id://916626]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://916626]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found