#!/usr/bin/perl -w use strict; use XML::Twig; my $twig= XML::Twig->new(); my $file = "message.xml"; $twig ->parsefile( $file ); my $root = $twig->root; my @all_text = gather_text($root); print join ("\n---", @all_text), "\n"; sub gather_text { my $node = shift; my (@children) = $node->children(); if (not @children) { # this tag has no children. grab its text data, with no # surrounding tag, and return it. return $node->sprint('NOTAGS'); } else { # recurse into each child my (@text); foreach (@children) { push @text, (gather_text($_)); } return @text; } }