dug has asked for the wisdom of the Perl Monks concerning the following question:
<Container> <Doc> <content>stuff</content> </Doc> <Doc> <content>different stuff</content> </Doc> ... </Container>
#!/usr/bin/perl use warnings; use strict; $|++; use XML::SAX::Machines qw( :all ); my $output_handle; # global stream output container ## # callback for end_document event. my $write_hook = sub { my $self = shift; my $current_doc = $output_handle; # get contents of output buffer $output_handle = ''; # clear buffer for next doc ## process current doc process_doc( $current_doc ); }; my $filter = EndDocumentAction->new(end_hook => $write_hook); my $machine = Pipeline( ByRecord( $filter ), \$output_handle, ); $machine->parse_file( \*DATA ); sub process_doc { my $content = shift; # do something interesting print $content, "\n"; } package EndDocumentAction; use base qw( XML::SAX::Base ); sub new { my ($class, %args) = @_; my $self = {}; $self->{End_Hook} = $args{end_hook}; # install callback for end_d +ocument $self->{start_counter} = 0; bless $self, $class; return $self; } sub end_document { my $self = shift; my $callback = $self->{End_Hook}; $self->$callback(); } 1; __END__ <Stream> <Doc> <foo>hey man</foo> </Doc> <Doc> <bar>hey man, how's it goin'?</bar> </Doc> <Doc> <baz>pretty right on.</baz> </Doc> </Stream>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML stream processing
by PodMaster (Abbot) on Nov 15, 2002 at 14:45 UTC |