本人在Windows系统下Eclipse工作的时候,输出xml文件的时候遇到了这么一个奇怪的编码问题。代码如下
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING,"GBK");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(file + "\\" + project.getName() + ".xml"));
transformer.transform(source, result);在Windows环境下我用EmEditor查看xml文件,编码为GB2132,但是当把文件移动Redhat(locale GBK)的时候,中文就是乱码。
但是我朦胧的把问题解决了,解决办法大致就是把此文件格式化一遍,采用了dom4j插件。
public static void formatXMLFile(String filePath, String encode) {
try {
SAXReader read = new SAXReader();
org.dom4j.Document doc = read.read(new File(filePath));
XMLWriter writer = null;
org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint();
format.setEncoding(encode);
writer = new XMLWriter(new FileOutputStream(new File(filePath)), format);
writer.write(doc);
writer.close();
}catch(Exception e) {
e.printStackTrace();
}
}
明明setOutputProperty方法就是明确设置了实际输出的编码格式啊,到底是怎么回事?
请赐教!
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING,"GBK");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(file + "\\" + project.getName() + ".xml"));
transformer.transform(source, result);在Windows环境下我用EmEditor查看xml文件,编码为GB2132,但是当把文件移动Redhat(locale GBK)的时候,中文就是乱码。
但是我朦胧的把问题解决了,解决办法大致就是把此文件格式化一遍,采用了dom4j插件。
public static void formatXMLFile(String filePath, String encode) {
try {
SAXReader read = new SAXReader();
org.dom4j.Document doc = read.read(new File(filePath));
XMLWriter writer = null;
org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint();
format.setEncoding(encode);
writer = new XMLWriter(new FileOutputStream(new File(filePath)), format);
writer.write(doc);
writer.close();
}catch(Exception e) {
e.printStackTrace();
}
}
明明setOutputProperty方法就是明确设置了实际输出的编码格式啊,到底是怎么回事?
请赐教!
解决方案 »
- js获得下拉列表的文本值?
- 有关js的一个很简单的问题。。。
- 我下了个maven-eclipse-plugin-1.12.jar,请问怎么用啊 我是想在eclipse里面建一个maven项目
- java如何获取SQLServer2005存储过程返回的多张表的结果集,高手指点
- 求救:weblogic 编译 jsp 出的错误
- 问一下weblogic7把由jsp编译出来的.java文件都放在哪里了?
- stusts arrayList表示
- 网站备案(我头都疼了)请大家支招
- weblogic初始安装的时候是不装成windows系统服务,现在怎么改成windows系统服务?
- 在实体bean中,能否返回HashMap之类值?
- 数组对象出现指针错误?
- IE和火狐的兼容
你可以试试。