use warnings; use strict; use XML::Twig; undef $/; my $XML = ; open my $OUT, '>', '/tmp/twig_out' or die "Can't open file: $!"; my $twig = XML::Twig->new( twig_handlers => { name => sub { ### If you want the text. if (1) { print $OUT $_->text(), "\n"; } ### If you want the elements. else { $_->print($OUT); } } } ); $twig->parse($XML); close $OUT or die $!; __DATA__