Created
April 16, 2013 07:01
-
-
Save mtsukamoto/5393929 to your computer and use it in GitHub Desktop.
My sample XML::SAX handler module.
With this sample handler, current path and path nodes' name are available in "characters" sub.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MySAXHandler; | |
use base qw(XML::SAX::Base); | |
# --- SYNOPSYS --- | |
# use XML::SAX; | |
# use MySAXHandler; | |
# my $parser = XML::SAX::ParserFactory->parser(Handler => MySAXHandler->new); | |
# $parser->parse_uri("sample.xml"); | |
sub start_document { | |
my ($self, $doc) = @_; | |
$self->{'nodes'} = []; | |
$self->{'path'} = '/'; | |
# process document start event | |
} | |
sub start_element { | |
my ($self, $el) = @_; | |
push(@{$self->{'nodes'}}, $el->{'LocalName'}); | |
$self->{'path'} = '/' . join('/', @{$self->{'nodes'}}); | |
# process element start event | |
} | |
sub characters { | |
my ($self, $chars) = @_; | |
# process characters event | |
print $self->{'path'} . "\n"; | |
print $chars->{'Data'} . "\n\n"; | |
} | |
sub end_element { | |
my ($self) = @_; | |
pop(@{$self->{'nodes'}}); | |
$self->{'path'} = '/' . join('/', @{$self->{'nodes'}}); | |
# process element end event | |
} | |
sub end_document { | |
my ($self) = @_; | |
# process document end event | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment