#!/usr/bin/perl -w
use strict;
use XML::Parser;
my( $in_item, $item);
my $parser= XML::Parser->new();
$parser->setHandlers( Start => \&start_handler,
Char => \&char_handler,
End => \&end_handler,
);
$parser->parse( \*DATA);
sub start_handler() # raises a flag when getting to Item
{
my ($p, $elmt ) = @_;
if ( $elmt eq 'Item' )
{ $in_item=1;
$item=''; # reset the item text
}
}
sub char_handler # stores the item text in $item
{
my( $p, $text)= @_;
if( $in_item) { $item .= $text; }
}
sub end_handler # processes the item and lowers the flag
{
my ($p, $elmt ) = @_;
if ( $elmt eq 'Item' )
{ $in_item=0; # Toto, we are not in Item anymore
# this is where you call the Item processing procedure
print "item: $item\n";
}
}
__DATA__
- A Book
####
#!/usr/bin/perl -w
use strict;
use XML::Twig;
my $twig= XML::Twig->new( twig_handlers => { Item => sub { print $_->text, "\n"; } });
$twig->parse( \*DATA);
__DATA__
- A Book