Skip to content

Instantly share code, notes, and snippets.

@korjavin
Created November 1, 2013 14:55
Show Gist options
  • Save korjavin/7266638 to your computer and use it in GitHub Desktop.
Save korjavin/7266638 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::SAX::ParserFactory;
use XML::SAX::PurePerl;
my $path=1;
my $path_text='';
my $prev_tag;
my $prev_node;
my $characters;
my @firstnames;
my $factory = new XML::SAX::ParserFactory;
my $handler = new XML::SAX::PurePerl;
my $parser = $factory->parser(
Handler => $handler,
Methods => {
characters => sub {
$characters = shift->{Data};
},
start_element => sub {
if( shift->{LocalName} eq 'ul' ){
$path +=1;
$path_text.='/' . $characters;
}
},
end_element => sub {
my $el=shift;
push @firstnames, ' / ' x $path . $characters if ($el->{LocalName} eq 'li');
if ($el->{LocalName} eq 'ul'){
$path -= 1;
}
}
}
);
$parser->parse_uri("3.html");
print Dumper \@firstnames;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment