码迷,mamicode.com
首页 > 其他好文 > 详细

dom4j 输出UTF-8 XML时中文乱码

时间:2018-07-08 11:49:56      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:enc   system   set   stream   puts   creat   sys   fill   style   

使用DOM4J的XMLWriter输出UTF-8编码的XML文件时,出现乱码

    public static void writToXml(Document document) throws IOException
    {
        OutputFormat format=OutputFormat.createPrettyPrint();
        XMLWriter writer=new XMLWriter(new FileOutputStream(fillpath),format);
        writer.write(document);//写入文件
        
        format=OutputFormat.createPrettyPrint();
        writer=new XMLWriter(System.out,format);//输出到屏幕
        writer.write(document);
    }

第二段代码在输出屏幕的时候,输出中文是乱码的。

修改如下后即输出中文了:

    public static void writToXml(Document document) throws IOException
    {
        OutputFormat format=OutputFormat.createPrettyPrint();
        XMLWriter writer=new XMLWriter(new FileOutputStream(fillpath),format);
        writer.write(document);
        
        format=OutputFormat.createPrettyPrint();
        format.setEncoding("gb2312");
        writer=new XMLWriter(System.out,format);
        writer.write(document);
    }

 

dom4j 输出UTF-8 XML时中文乱码

标签:enc   system   set   stream   puts   creat   sys   fill   style   

原文地址:https://www.cnblogs.com/alsf/p/9279519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!