#!/usr/bin/perl -w use warnings; use strict; use XML::Twig; my $xml = <This is an id Title stuff Another title XML my $twig = XML::Twig->new( twig_roots => { ID => \&dump, TimeStamp => \&dump, IP_Address => \&dump, 'Book1/Title' => sub {title (1, @_);}, 'Book2/Title' => sub {title (2, @_);}, Complainant => \&dump, } ); $twig->parse($xml); sub dump { my ($t, $elt) = @_; (my $text = $elt->text()) =~ s/^\s+|\s+$//g; print "$text\n"; } sub title { my ($type, $t, $elt) = @_; (my $title = $elt->text()) =~ s/^\s+|\s+$//g; print "Title $type: $title\n"; }