首页 > 其他 > 详细

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

时间:2018-07-08 11:40:33      阅读:272      评论:0      收藏:0      [点我收藏+]

使用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时中文乱码

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!