开发者

Perl code Save ANSI encoding format xml file into UTF-8 encoding

I need to change the encoding format of a file from ANSI to UTF-8... Please suggest me to complete this, I have done using some methods. But it didn't work. Herewith I have written the code, which I have did.

use utf8;
use File::Slurp;

$File_Name="c:\\test.xml";
$file_con=read_file($File_Name);

open (OUT, ">c:\\b.xml");
binmode(OUT, ":utf8");
print开发者_开发技巧 OUT $file_con;
close OUT;


Assuming you have a valid XML file, this would do it:

use XML::LibXML qw( );

my $doc = XML::LibXML->new()->parse_file('text.xml');
$doc->setEncoding('UTF-8');

open(my $fh, '>:raw', 'test.utf8.xml')
   or die("Can't create test.utf8.xml: $!\n");
print($fh $doc->toString());

This handles both converting the encoding and adjusting the <?xml?> directive. The previous answers left the wrong encoding in the <?xml?> directive.


If you just want to make a filter, try this:

perl -MEncode -pwe 's/(.*)/encode('utf8', $1)/e;'

For example:

type c:\text.xml |perl -MEncode -pwe 's/(.*)/encode('utf8', $1)/e;' >c:\b.xml

Or modifying your code:

use File::Slurp;
use Encode;

$File_Name="c:\\test.xml";
$file_con=read_file($File_Name);

open (OUT, ">c:\\b.xml");
print OUT encode('utf8', $file_con);
close OUT;


Use Text::Iconv:

use Text::Iconv;
$converter = Text::Iconv->new("cp1252", "utf-8");
$converted = $converter->convert($file_con);

(assuming you are using codepage 1252 as your default codepage).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜