【原創】JDom輸出UTF-8的XML完美解決(中文亂碼的原因分析)
阿新 • • 發佈:2019-01-25
JDom輸出UTF-8的XML完美解決(中文亂碼的原因分析)
現象描述:JDom輸出Xml檔案,當使用字元編碼GBK時正常,而輸出UTF-8時亂碼。 完美的解決方法從闢謠開始: 1)JDOM是否生成UTF-8的檔案與Format是否設定無關,只有輸出其他字元編碼才需要設定,見下面的註釋。 2)JDOM輸出UTF-8檔案亂碼的根本原因並非在JDOMAPI,而是在JDK。 具體描述: JDOM的輸出類XMLOutputter有兩個output介面,除了都具有一個Document引數外,分別接受Writer和OutputStream引數。 這給我們一個錯覺,兩個介面可以任意使用。 首先我們用output(doc,System.out)來做測試,此時得到亂碼, 然後我們改為output(doc,new PrintWriter(System.out))來測試,輸出不是亂碼, 也就是說在控制檯的時候一定要用一個Writer介面包裝一下。 然後我們用output(doc,new FileWriter(path))來做測試,結果卻得到亂碼, 然後我們改為output(doc,new FileOutputStream(path))來測試,輸出不是亂碼, 也就是說在輸出檔案的時候一定要用一個OutputStream介面包裝一下。 瘋了吧?呵呵,很搞笑是吧。經過到JDOM的原始碼中除錯,發現沒有任何問題,問題出在了JDK裡面。 JDK內的對應介面處理: 1)PrintWriter類有引數為OutputStream的構造方法,因此可以從System.out包裝到PrintWriter 2)FileWriter類沒有引數為OutputStream的構造方法,因此不能從FileOutputStream包裝到FileWriter 3)如果PrintWriter類用了引數為Writer的構造方法(Writer實現為FileWriter),最後輸出也是亂碼 4)如果用一個FileOutputStream來包裝一個控制檯輸出,也是亂碼 因此,對於JDK內的各種輸出體系,各種InputStream、OutputStream、reader和writer要充分認識,否則極容易出現一些意想不到的問題。 測試的JDOM版本:1.0、1.1 測試程式碼:=========================================================== 王琦 系統架構師 國信朗訊科技網路技術有限公司 Guoxin Lucent Technologies Network Technologies Co.,Ltd.import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.PrintWriter; import java.util.HashMap; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class BuildXML { public static void main(String[] args) throws Exception{ File xmlfile=new File("C://EditTemp//xml//abc.xml"); //中文問題 //GBK 是沒有問題的,但UTF-8就是有問題的 //原因: //1)對於磁碟檔案,必須使用輸出流 FileOutputStream // FileWriter out=new FileWriter(xmlfile);會導致亂碼 //2)對於控制檯輸出,則必須使用PrintWriter,如果直接使用System.out也會出現亂碼 // PrintWriter out=new PrintWriter(System.out); FileOutputStream out=new FileOutputStream(xmlfile); Element eroot=new Element("root"); eroot.addContent((new Element("code")).addContent("程式碼")); eroot.addContent((new Element("ds")).addContent("資料來源")); eroot.addContent((new Element("sql")).addContent("檢索sql")); eroot.addContent((new Element("order")).addContent("排序")); Document doc=new Document(eroot); XMLOutputter outputter = new XMLOutputter(); //如果不設定format,僅僅是沒有縮排,xml還是utf-8的,因此format不是必要的 Format f = Format.getPrettyFormat(); //f.setEncoding("UTF-8");//default=UTF-8 outputter.setFormat(f); outputter.output(doc, out); out.close(); } }