您的位置:首页 > 其它

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

2010-06-29 16:55 369 查看

使用DOM4J的XMLWriter输出UTF-8编码的XML文件时,出现乱码。

XMLWriter xmlWriter = null;
try {
FileWriter fw = new FileWriter("message.xml");
OutputFormat outFormat = OutputFormat.createPrettyPrint();
outFormat.setEncoding("UTF-8");
outFormat.setTrimText(false);
xmlWriter= new XMLWriter(fw,outFormat);
xmlWriter.write(doc);

} catch (IOException e) {
e.printStackTrace();
}finally{
if(xmlWriter!=null)
xmlWriter.close();

}
doc如果含有中文,会出现乱码的问题,将上面的FileWriter改成FileOutputStream便可以了。

XMLWriter xmlWriter = null;
try {
OutputFormat outFormat = OutputFormat.createPrettyPrint();
outFormat.setEncoding("UTF-8");
outFormat.setTrimText(false);
xmlWriter= new XMLWriter(new FileOutputStream("message.xml"),outFormat);
xmlWriter.write(doc);

} catch (IOException e) {
e.printStackTrace();
}finally{
if(xmlWriter!=null)
xmlWriter.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: