#!/usr/bin/perl use warnings; use strict; use XML::Twig; my $xmlFile = 'demo.xml'; my $t = XML::Twig->new( keep_atts_order => 1, pretty_print => 'indented_a', twig_handlers => { 'myconfig/myobj/myattributes/attribB' => \&getfoo, }, ); $t->parsefile($xmlFile); $t->print; sub getfoo { my ($t, $e) = @_; my $id = $e->parent->parent->first_child_text('uniqueName'); if ('objB' eq $id) { $e->set_text(' NEW '); } } #### open demo.xml ; set /myconfig/myobj[uniqueName='objB']/myattributes/attribB 'NEW' ; save :b ;