use warnings; use 5.014;
our $doc = q{
Majd az édes álom pillangó képében
Elvetődött arra tarka köntösében,
De nem mert szemére szállni még sokáig,
Szinte a pirosló hajnal hasadtáig.
Mert félt a szunyogtól, félt a szúrós nádtól,
Jobban a nádasnak csörtető vadától,
Félt az üldözőknek távoli zajától,
De legis-legjobban Toldi nagy bajától.
};
####
our %xmlopt = (
keep_spaces => 1, comments => "drop",
);
binmode STDOUT, ":encoding(iso-8859-2)";
if (1) {
my $n;
my $tw;
my $la_handler = sub {
my($tw1, $la) = @_;
if ($n++ < 2) {
print "In the handler for la elements. So far, the document tree contains this: (((\n" .
$tw->sprint .
"\n)))\n";
}
1;
};
$tw = XML::Twig->new(
twig_handlers => {"la" => $la_handler},
%xmlopt,
);
$tw->parse($doc);
}
##
##
=begin output
In the handler for la elements. So far, the document tree contains this: (((
Majd az édes álom pillangó képében
Elvetődött
)))
In the handler for la elements. So far, the document tree contains this: (((
Majd az édes álom pillangó képében
Elvetődött arra tarka köntösében,
De nem mert
)))
=end output
=cut
##
##
if (1) {
my $tw = XML::Twig->new(%xmlopt);
$tw->parse($doc);
for my $la ($tw->findnodes("//la")) {
my $t = $la->text;
my $ta = $la->next_sibling_text;
print "Found an la element. Its text is ((($t))). The text immediately after is (($ta))).\n";
}
}
##
##
=begin output
Found an la element. Its text is (((Elvetődött))). The text immediately after is (( arra tarka köntösében,))).
Found an la element. Its text is (((mert))). The text immediately after is (( szemére szállni még sokáig,))).
Found an la element. Its text is (((félt))). The text immediately after is (( a szunyogtól, ))).
Found an la element. Its text is (((félt))). The text immediately after is (( a szúrós nádtól,))).
Found an la element. Its text is (((Félt))). The text immediately after is (( az üldözőknek távoli zajától,))).
=end output
=cut
##
##
if (1) {
my $line_handler = sub {
my($tw1, $li) = @_;
print "In the line handler. Full line is (((" . $li->sprint . ")))\n";
for my $la ($li->findnodes("//la")) {
my $t = $la->text;
my $ta = $la->next_sibling_text;
print "Found an la element. Its text is ((($t))). The text immediately after is (($ta))).\n";
}
$tw1->purge;
};
my $tw = XML::Twig->new(
twig_handlers => {"line" => $line_handler},
%xmlopt
);
$tw->parse($doc);
}
##
##
=begin output
In the line handler. Full line is (((Majd az édes álom pillangó képében)))
In the line handler. Full line is (((Elvetődött arra tarka köntösében,)))
Found an la element. Its text is (((Elvetődött))). The text immediately after is (( arra tarka köntösében,))).
In the line handler. Full line is (((De nem mert szemére szállni még sokáig,)))
Found an la element. Its text is (((mert))). The text immediately after is (( szemére szállni még sokáig,))).
In the line handler. Full line is (((Szinte a pirosló hajnal hasadtáig.)))
In the line handler. Full line is (((Mert félt a szunyogtól, félt a szúrós nádtól,)))
Found an la element. Its text is (((félt))). The text immediately after is (( a szunyogtól, ))).
Found an la element. Its text is (((félt))). The text immediately after is (( a szúrós nádtól,))).
In the line handler. Full line is (((Jobban a nádasnak csörtető vadától,)))
In the line handler. Full line is (((Félt az üldözőknek távoli zajától,)))
Found an la element. Its text is (((Félt))). The text immediately after is (( az üldözőknek távoli zajától,))).
In the line handler. Full line is (((De legis-legjobban Toldi nagy bajától.)))
=end output
=cut