Re: Shortcutting a SAX parser
Michael Ludwig <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Emmanuel Rodriguez schrieb:
> Hi Michael,
>
> There are many kinds of XML parsers: DOM (tree based and memory pig),
> SAX (event based and uses less memory) and Pull parsers.
> See
> http://www.programmersheaven.com/user/pawanspace/blog/609-XML-parsers-Push-versus-Pull-parsers/
Hi Emmanuel,
thanks a lot for these news - to me :-) I had always wondered what kind
of beast a pull parser might be, but obviously my curiosity had never
grown strong enough in order to make me start to research the matter.
> What you want is a Pull parser, it consists of an XML parser that will
> parse the document only at you request. Once you find the element(s)
> you need you can close the parser. There's no need to parse the whole
> file.
>
> LibXML provides a pull parser (
> http://search.cpan.org/perldoc?XML::LibXML::Reader). I've never used
> it but I'm sure that others can help you with this.
This parser significantly reduces my code. See below. Thanks!
Michael Ludwig
use strict;
use warnings;
use XML::LibXML::Reader; # Pull-Parser
my %options;
# %options = ( expand_entities => 1, load_ext_dtd => 1);
while ( <> ) {
chomp;
my $reader = XML::LibXML::Reader->new( %options, location => $_)
or die "Kann $_ nicht lesen";
$reader->nextElement( 'TVChannel');
print join( "\t",
map $reader->getAttribute( $_), qw(TVChannelID TVChannelName)
), "\n";
$reader->close;
}
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs