使用 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);
- }
来源: http://www.bubuko.com/infodetail-2676549.html