use strict; use warnings; use XML::LibXML qw( ); my $xml = 'í'; my $parser = XML::LibXML->new(); my $doc = $parser->parse_string($xml); $doc->setEncoding('UTF-8'); open(my $fh, '>:bytes', 'xml') or die; print($fh $doc->toString);