#!/usr/bin/perl -w use strict; use XML::Twig; my $twig = XML::Twig->new( pretty_print => 'indented'); $twig->parse( \*DATA); # the * means that the nodes returned will be either #PCDATA # or #CDATA, this would not work if the content of text was... # not text but included sub elements foreach my $node ($twig->find_nodes('/foo/bar/text/*')) { my $data = your_munge_function($node->text); $node->set_text( $data); } $twig->print; sub your_munge_function { return "munged $_[0]"; } __DATA__ Foo bar baz]]> a normal text>