use warnings; use strict; use XML::Twig; my $str = < John 001 A John 002 C EOF my $t = XML::Twig->new( twig_handlers => { student => \&student, }, pretty_print => 'indented', ); $t->parse($str); $t->print($str); print "\n"; sub student { my ($t, $elt) = @_; if ($elt->field('id') eq '002') { $elt->first_child('gpa')->set_text('B'); } } __END__ John 001 A John 002 B