Previously, to read XML in UTF-8 encoding through xstream, I am using DomDriver as follow :
XStream xStream = new XStream(new DomDriver("UTF-8"));
However, later I realize this is VERY slow. I use the following way :
http://stackoverflow.com/questions/3623546/optimize-loading-speed-of-xstream
This works fine at least.
However, later, I realize the same technique cannot be applied to write XML. I will get all ??? characters.
This is the last workable code using DomDriver during write
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;
}
The above code works fine. In order to match with the read method which doesn't use DomDriver, I change the code to
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;
}
This time, all my Chinese characters changes to ???
May I know anything I had done wrong?