trans.transform()输出XML乱码的解决办法

将dom输出xml文件代码是这样写的,在xml 编码是 utf-8的是侯,一般不会出错乱码。但我们GB2321的时候,所有的汉字必定成为乱码。
    TransformerFactory  transf = TransformerFactory.newInstance();
     Transformer trans=transf.newTransformer();
     Source in=new DOMSource(document);
     Result out=new FileOutputStream("hzlzh_06.xml");
     trans.transform(in,out);
解决方法如下:
用    OutputStreamWriter()这个函数来说明代码格式
更改倒数第二句即可!
Result out=new StreamResult(new OutputStreamWriter(
                           new FileOutputStream("hzlzh_06.xml"),"GB2312"));
自此再也不会乱码了。
版权所有© HzlzH | 本文采用 BY-NC-SA 进行授权
转载需注明 转自: 《trans.transform()输出XML乱码的解决办法

随机文章

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)