perl xml dom中文亂碼問題解決

2021-09-08 16:39:28 字數 1183 閱讀 4059

在linux伺服器上,很容易出現中文亂碼。

一般情況下,只要保持伺服器環境是utf8,檔案格式是utf8,用各種語言預設的讀寫檔案方式通常不會出現亂碼。

但是,在用perl xml dom寫xml的時候就出現了這個問題。

perl xml dom預設應該是用ascii來讀寫檔案,所以,對中文要先進行解碼decode,再寫入。

例如:

1

#更新顯示的xml檔案

2sub

writexml

3); 8my

$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

);1516#

write the file

17my

$xml = ($doc->createxmldecl('

1.0','

utf-8

')->tostring).($root->tostring) ;

1819

open

my$myfd, "

>

", $projectlist;20

print

$myfd

$xml;21

close

$myfd;22

23#xml tidy

24my

$tidy_obj = xml::tidy->new('

filename

' => $projectlist

);25

$tidy_obj->tidy();

26$tidy_obj->write

();27 }

webView loadData 中文亂碼問題

使用 loaddata方法是中文部分會出現亂碼,即使指定 utf 8 gbk gb2312 也一樣。webview.getsettings setdefaulttextencodingname utf 8 設定預設為utf 8 text html utf 8 api提供的標準用法,無法解決亂碼問題 ...

mysql jsp servlet 中文亂碼問題。

google一下。你可以找一大堆解決方案。但是我覺得都沒有從根本上解決問題。是不是?我覺得要想根本解決。最主要的方法是統一編碼方案。說出來很簡單。原因也很簡單。但你真的做到了嗎。首先不論你寫的是jsp servlet html 等都必須顯示標明contenttype為utf 8 強烈推薦是utf 8...

jsp,tomcat,MYSQL下中文亂碼問題

這些天除錯開發乙個lucene專案,伺服器是tomcat,資料庫是mysql5,作業系統是xp,採用的是mvc架構,模式是採用facade模式,總是出現亂碼,頁面字符集 資料庫編碼 tomcat 專案編碼全部都是utf 8,可是沒想到表單裡提交的資料裡的中文還是有亂碼,經過思考,發現通過簡單的資料庫...