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();
}
相关文章推荐
- dom4j 输出UTF-8 XML时中文乱码
- JDom输出UTF-8的XML出现中文乱码完美解决
- 【原创】JDom输出UTF-8的XML完美解决(中文乱码的原因分析)
- dom4j解析utf-8 的xml出现中文乱码的为
- utf-8编码引起js输出中文乱码的解决办法
- XML输出中文时,无法用xsl查看(XML文件不能正常显示、中文显示乱码)
- php输出UTF-8格式XML文件中文乱码问题(含asp,asp.net,jsp代码)
- utf-8编码引起js输出中文乱码的解决办法
- utf-8编码引起js输出中文乱码的解决办法
- 乱码转中文输出(ISO-8859-1 to UTF-8)
- dom4j处理xml在linux环境下中文乱码
- 用dom4j建立,修改XML文档,并解决格式化输出和中文问题
- jDom 和dom4j 输出的中文乱码的解决方案
- XML输出中文时,无法用xsl查看(XML文件不能正常显示、中文显示乱码)
- 【转载】utf-8编码引起js输出中文乱码的解决办法
- java 解析xml是使用dom4j空行以及中文乱码的处理
- Dom4j导出xml,中文乱码
- UTF-8编码导致JS输出中文乱码
- “用dom4j解析包”对xml进行dom方式操作中文乱码解决方案
- MFC/C++ CFile写入文件数据,输出utf-8的文本。(防止中文乱码)