以前,通过xstream以UTF-8编码读取 XML,我使用的是DomDriver:
XStream xStream = new XStream(new DomDriver("UTF-8"));
然而,后来我意识到这非常慢。我使用以下方式:
Optimize loading speed of xstream
这至少可以正常工作。
但是,后来,我意识到同样的技术不能应用于编写XML。我会得到所有???字符。
这是在写
期间使用DomDriver的最后一个可行代码public static boolean toXML(Object object, File file) {
XStream xStream = new XStream(new DomDriver("UTF-8"));
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
xStream.toXML(object, outputStream);
}
catch (Exception exp) {
log.error(null, exp);
return false;
}
finally {
if (false == close(outputStream)) {
return false;
}
outputStream = null;
}
return true;
}
上面的代码工作正常。为了与不使用DomDriver的读取方法匹配,我将代码更改为
public static boolean toXML(Object object, File file) {
XStream xStream = new XStream();
OutputStream outputStream = null;
Writer writer = null;
try {
outputStream = new FileOutputStream(file);
writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
xStream.toXML(object, outputStream);
}
catch (Exception exp) {
log.error(null, exp);
return false;
}
finally {
if (false == close(writer)) {
return false;
}
if (false == close(outputStream)) {
return false;
}
writer = null;
outputStream = null;
}
return true;
}
这一次,我所有的汉字都改为???
我可能知道我做错了什么吗?
答案 0 :(得分:11)
看看这段代码:
outputStream = new FileOutputStream(file);
writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
xStream.toXML(object, outputStream);
你创建一个将使用UTF-8的作家 - 然后完全忽略它!
请改为尝试:
xStream.toXML(object, writer);
另外,作为一种风格问题,我建议您考虑以下事项:
if (foo)
或if (!foo)
代替Exception
非常很少是一个好主意;抓住特定的例外close
失败,您将在第二次close
调用之前退出该方法,这可能不是您想要的。 (事实上,关闭OutputStreamWriter`无论如何都会关闭流,但要考虑事情的原理。)null
是不必要的,并且会使代码混乱