zoukankan      html  css  js  c++  java
  • perl xml dom中文乱码问题解决

    在linux服务器上,很容易出现中文乱码。

    一般情况下,只要保持服务器环境是utf8,文件格式是utf8,用各种语言默认的读写文件方式通常不会出现乱码。

    但是,在用perl xml dom写xml的时候就出现了这个问题。

    perl xml dom默认应该是用ascii来读写文件,所以,对中文要先进行解码decode,再写入。

    例如:

     1 #更新显示的xml文件
     2 sub writeXml
     3 {
     4     my $projectList = "$hometouch_root/config/projectList.xml";
     5     my $parser = new XML::DOM::Parser ;
     6     my $doc = $parser->parsefile($projectList);
     7     XML::DOM::setTagCompression(sub{return 1}); 
     8     my $newItem = $doc->createElement("item");
     9     #所以这里先将中文进行解码
    10     $newItem->setAttribute("name",decode("utf8",$projectChinese));
    11     $newItem->setAttribute("eng",$projectName);
    12     $newItem->setAttribute("version","");
    13     my $root = $doc->getElementsByTagName("projects")->[0];
    14     $root->appendChild($newItem);
    15     
    16     #write the file
    17     my $xml = ($doc->createXMLDecl('1.0','UTF-8')->toString).($root->toString) ;
    18     
    19     open my $myfd, ">", $projectList;
    20     print $myfd $xml ;
    21     close $myfd ;
    22     
    23     #xml tidy
    24     my $tidy_obj = XML::Tidy->new('filename' => $projectList);
    25     $tidy_obj->tidy();
    26     $tidy_obj->write();
    27 }
  • 相关阅读:
    Cookie
    C#计算程序的运行时间
    .Net源码之Page类
    AJAX资源
    匿名方法的使用
    序列化和反序列化
    实验分析C#中三种计时器使用异同点
    (转载)突然就看懂了《大话西游》
    做人、做事,做架构师——架构师能力模型解析
    服务颗粒度的困扰(转)
  • 原文地址:https://www.cnblogs.com/trying/p/2955867.html
Copyright © 2011-2022 走看看