use strict; use warnings; use English; use XML::Twig; my $xmlFile = "demo.xml"; my $t = XML::Twig->new( keep_atts_order => 1, pretty_print => 'indented_a', twig_handlers => { q(myconfig/myobj[@uniqueName=objA]/myattributes/attribB) => \&getfoo, } ); $t->parsefile( $xmlFile ); $t->print; exit 0; sub getfoo { my ($t, $e) = @_; print "text=", $e->text(), "' tag='", $e->tag(), "' path='", $e->path(), "' parentTag='", $e->parent->tag(), "'\n"; } #### text= fooB ' tag='attribB' path='/myconfig/myobj/myattributes/attribB' parentTag='myattributes' text= fooB ' tag='attribB' path='/myconfig/myobj/myattributes/attribB' parentTag='myattributes' objA fooA fooB objB fooA fooB