DOM4J 生成中文XML出现乱码
时间:2011-04-26 来源:李克华
1 使用FileWriter写中文xml出现乱码 很怪异。
XMLWriter writer=null;
try{
Document doc=DocumentHelper.parseText(result);
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
//FileOutputStream fos=new FileOutputStream(new File("C:\\GeoSearchResponse.xml"));
writer=new XMLWriter(new FileWriter(new File("C:\\GeoSearchResponse.xml")),format);
//writer=new XMLWriter(fos,format);
writer.write(doc);
System.out.println("xml:"+doc.asXML());
}catch(DocumentException w){
w.printStackTrace();
}catch(IOException i){
i.printStackTrace();
}finally{
if(writer!=null){
try{
writer.close();
}catch(IOException e){
e.printStackTrace();
};
};
};
2 经过反复测试和实验 改成FileOutputStream来写 结果良好
XMLWriter writer=null;
try{
Document doc=DocumentHelper.parseText(result);
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
FileOutputStream fos=new FileOutputStream(new File("C:\\GeoSearchResponse.xml"));
//writer=new XMLWriter(new FileWriter(new File("C:\\GeoSearchResponse.xml")),format);
writer=new XMLWriter(fos,format);
writer.write(doc);
System.out.println("xml:"+doc.asXML());
}catch(DocumentException w){
w.printStackTrace();
}catch(IOException i){
i.printStackTrace();
}finally{
if(writer!=null){
try{
writer.close();
}catch(IOException e){
e.printStackTrace();
};
};
};
- 系统休眠文件删除后果 如何删除计算机的休眠文件 2025-04-22
- 站群服务器是什么意思 站群服务器的作用 站群服务器和普通服务器的区别 2025-04-22
- jQuery插件有何作用 jQuery插件的使用方法 2025-04-22
- jQuery插件有哪些种类 简单的jQuery插件实例 2025-04-22
-