XML technologies / XML / Perl & XML / SAX / A handler base class
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
SAX does not distinguish different elements; that task is left to you. For example, the handler start_element( ) reads the element name and pushes it onto a stack to track the hierarchy. This repetitive work invites abstraction, which is what Ken MacLeod’s module XML::Handler::Subs provides.
The module defines an object delegating handler calls to more specialized handlers. Want a handler for only <title> elements? Write it, and the module will call it. Selection is based on handler names: start-tag handlers have names beginning with s_ . The prefix is followed by the element name, with special characters replaced by underscores. For example, s-start-tag would not be a valid Perl method name. End-tag handlers similarly begin with e_.
The base object also includes a built-in stack and an accessor method for checking whether you are inside a given element. The stack is available through $self->{Names}, and the method is called as in_element( $name ) to determine whether you are inside an element $name.
Let us try it with a program performing element-specific work. We read an HTML file and print everything inside a <h1> element, including any emphasis elements. The source code is remarkably simple.
Example: a parser derived from XML::Handler::Subs
use XML::Parser::PerlSAX;
use XML::Handler::Subs
#
# Initialisierung des Parsers
#
use XML::Parser::PerlSAX;
my $parser = XML::Parser::PerlSAX->new( Handler => H1_grabber->new( ) );
$parser->parse( Source => {SystemId => shift @ARGV} );
## Das Handlerobjekt: H1_grabber
##
package H1_grabber;
use base( 'XML::Handler::Subs' );
sub new {
my $type = shift;
my $self = {@_};
return bless( $self, $type );
}
#
# Handler für Start des Dokuments
#
sub start_document {
SUPER::start_document( );
print "Inhaltsverzeichnis der Datei:\n";
}
#
# Start von <h1> behandeln: Gib ein Markierungszeichen aus
#
sub s_h1 {
print "[";
}
#
# Ende von <h1>: weiteres Markierungszeichen und Zeilenende
#
sub e_h1 {
print "]\n";
}
#
# Behandlung von Textdaten
#
sub characters {
my( $self, $props ) = @_;
my $data = $props->{Data};
print $data if( $self->in_element( h1 ));
}Feed the program a test file:
<html>
<head><title>Das Leben des Brian</title></head>
<body>
<h1>Brians Mama</h1>
<p>...</p>
<h1>Die Steinigung</h1>
<p>...</p>
<h1>Brian trifft <em>Judith</em>!</h1>
<p>...</p>
</body>
</html>Here is the result:
Inhaltsverzeichnis der Datei:
[Brians Mama]
[Die Steinigung]
[Brian trifft Judith!] Thanks to the call to in_element( ) even text inside an <em>-element is visible. XML::Handler::Subs is certainly useful when working with SAX.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top