XML::Parser is an event-based parser, which means that you must register event handlers (callbacks) with the parser to process an XML document. As the document is parsed, the event handlers are called when the corresponding events are detected.
There are several handlers, but most applications will only need the Start, End, and Char handlers. See the module documentation for a list of all the handlers.
% ./configure % make # make install
% perl Makefile.PL % make % make test # make install
<?xml version="1.0"?> <country> <name abbrv="fr">France</name> <capital>Paris</capital> <language>French</language> </country>
#!/usr/bin/perl -w use strict; use XML::Parser; die "Usage: xmlparser1.plRunning this script," unless @ARGV == 1; my $file = shift; die qq!Can't find file "$file"! unless -f $file; my $parser = new XML::Parser( Handlers => {Start => \&start_handler, End => \&end_handler, Char => \&char_handler)); $parser->parsefile($file); ########################## # Handlers # ########################## sub start_handler { my $expat = shift; my $element = shift; print "START: $element\n"; # Handle the attributes while (@_) { my $attr = shift; my $val = shift; print qq!ATTR: $attr="$val"\n!; } } sub end_handler { my($expat, $element) = @_; print "END: $element\n"; } sub char_handler { my($expat, $data) = @_; return if $data =~ /^\s+$/; print "CHAR: $data\n"; }
% perl xmlparser1.pl country.xmlwill produce the following output:
START: country START: name ATTR: abbrv="fr" CHAR: France END: name START: capital CHAR: Paris END: capital START: language CHAR: French END: language END: country
#!/usr/bin/perl -w use strict; use XML::Parser; die "Usage: xmlparser1.pl" unless @ARGV == 1; my $file = shift; die qq!Can't find file "$file"! unless -f $file; my $parser = new XML::Parser(ErrorContext => 2); $parser->setHandlers(Start => \&start_handler, End => \&end_handler, Char => \&char_handler); $parser->parsefile($file); ########################## # Handlers # ########################## sub start_handler { my $expat = shift; my $element = shift; my %attribs = @_; if ( $element =~ /\bfirst\b/i ) { print "First Name: "; } elsif ( $element =~ /\blast\b/i ) { print "Last Name: "; } elsif ( $element =~ /\bstreet\b/i ) { print "Street: "; } elsif ( $element =~ /\bcity\b/i ) { print "City: "; } elsif ( $element =~ /\bstate\b/i ) { print "State: "; } elsif ( $element =~ /\bzip\b/i ) { print "Zip: "; } elsif ( $element =~ /\btel\b/i ) { print "Phone ($attribs{'type'}): "; } elsif ( $element =~ /\bemail\b/i ) { print "Email: "; } } sub end_handler { my($expat, $element) = @_; if ( $element =~ /\bcontact\b/i ) { print "\n"; } } sub char_handler { my($expat, $data) = @_; return if $data =~ /^\s+$/; print "$data\n"; }