使用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); }