Data::Stag::BaseHandler is a simple event handler, other handlers inherit from this class.
Stag has an event-handling architecture; parsers or generators generate or fire events. Events can be hierarchical/nested, just like stag nodes. These events are caught by handlers. By default, uncaught events stack to form stag trees.
Stag has built in parsers for parsing xml, sxpr and itext data. You can construct your own parsers for dealing with your own formats specific to your own data; these should inherit from Data::Stag::BaseGenerator
Stag also has built in handlers for these formats. You can construct your own - either as modules that inherit from this one, or as hashes of anonymous subroutines.
If you wish to write your own handler that writes out to another format, you may wish to inherit from Data::Stag::Writer
SYNOPSIS
# EXAMPLE 1
package MyPersonHandler;
use base qw(Data::Stag::BaseHandler);
# handler that prints < person > nodes as they are parsed;
# after each < person > node is intercepted, it is discarded
# (it does not go to form the final tree)
sub e_person {
my $self = shift;
my $node = shift;
printf "Person name:%s address:%s
",
$node->sget('name'), $node->sget('address');
return; # prune this from tree
}
1;
# EXAMPLE 2
package MyStatsHandler;
use base qw(Data::Stag::BaseHandler);
# handler that modifies tree as it goes
# changes < measurement >< unit >inch< /unit >< quantity >10< /quantity >< /measurement >
# to < measurement >< unit >cm< /unit >< quantity >25< /quantity >< /measurement >
sub e_measurement {
my $self = shift;
my $node = shift;
if ($node->sget('unit') eq 'inch') {
$node->set('unit', 'cm');
$node->set('quantity', $node->get('quantity') * 2.5);
}
return $node; # replace < measurement > with new data in result tree
}
1;
# Using the handlers
my $handler = MyHandler->new;
my $stag = Data::Stag->parse(-fh=>$fh, -handler=>$handler);
# Using a handler from the command line:
unix> stag-handle.pl -m MyHandler input.xml > post-processed.xml
Requirements:
· Perl