以前,要讀取UTF-8編碼 XML通過XStream的,我使用DomDriver如下:XStream的 - 正確的方式來保存XML的UTF-8
XStream xStream = new XStream(new DomDriver("UTF-8"));
然而,後來我意識到這是非常慢。我用下面的方法:
Optimize loading speed of xstream
這工作至少罰款。
但是,後來我意識到相同的技術不能用於寫入XML。我會得到所有?字符。
這期間寫
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最後一個可行的代碼。爲了配合與讀方法不使用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;
}
這一次,我所有的中國角色的變化???
我可以知道我做錯了什麼嗎?
喜 - 你在上述方法中作爲「對象」傳遞什麼? – ziggy 2014-02-17 19:01:21