use strict; use warnings qw( all ); use feature qw( say ); use XML::LibXML qw( ); my $xml = <<'__EOS__'; Need to export this text __EOS__ my $doc = XML::LibXML->new->parse_string($xml); for my $foo_node ($doc->findnodes('/seg/foo')) { my $mid = $foo_node->getAttribute('mid'); my $inner_xml = join('', $foo_node->childNodes); say "$mid $inner_xml"; }