最近在项目中要求做xml的转换,要求生成编码是utf-8的是,在生成时
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("UTF-8");
这个虽然设置了编码,但是生成完后使用nodepad++打开还是ansi格式打开的,如果使用utf-8就会显示乱码,在linux上更明显,要想让生成的文件为utf-8格式的需要使用下面的方式
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file),“utf-8”);
document.setXMLEncoding("UTF-8");
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
在写入流文件时设置编码即可。