OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(XmlPath), format);
writer.write(doc);
writer.close();
保证输出的xml被格式化,同时输出没有自动转换为ANSI。
如使用XMLWriter writer = new XMLWriter(new FileWriter(new File(XmlPath)), format);会出现Xml文件第一行是UTF-8,但是内容确实ANSI的乱码状态。