use XML::LibXML; my $xml = q| Need to export this text Need to export this text also |; my $doc = XML::LibXML->load_xml(string => $xml); my @foos = $doc->findnodes('//foo'); for my $foo (@foos) { my $mid = $foo->getAttribute('mid'); print "mid: $mid "; my @childnodes = $foo->childNodes(); if (@childnodes) { for my $node (@childnodes) { print $node->toString, "\n"; } } else { print $foo->textContent, "\n"; } }