Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

XML::SAX::ExpatXS - stop parsing?

by Jaap (Curate)
on Jul 25, 2011 at 21:40 UTC ( #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?

Comment on XML::SAX::ExpatXS - stop parsing?
Select or Download Code
Re: XML::SAX::ExpatXS - stop parsing?
by ikegami (Pope) 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
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? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-12-22 14:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (119 votes), past polls