java xstream - 以 UTF-8 格式保存 XML 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3642820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xstream - correct way to save XML in UTF-8
提问by Cheok Yan Cheng
Previously, to readXML in UTF-8 encoding through xstream, I am using DomDriver as follow :
以前,为了通过 xstream读取UTF-8 编码的 XML,我使用 DomDriver 如下:
XStream xStream = new XStream(new DomDriver("UTF-8"));
However, later I realize this is VERY slow. I use the following way :
但是,后来我意识到这很慢。我使用以下方式:
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.
然而,后来,我意识到同样的技术不能应用于编写 XML。我会得到所有???人物。
This is the last workable code using DomDriver during write
这是在写入期间使用 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;
}
The above code works fine. In order to match with the readmethod which doesn't use DomDriver, I change the code to
上面的代码工作正常。为了配合不使用DomDriver的read方法,我把代码改成
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?
我可以知道我做错了什么吗?
回答by Jon Skeet
Look at this code:
看看这段代码:
outputStream = new FileOutputStream(file);
writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
xStream.toXML(object, outputStream);
You're creatinga writer which will use UTF-8 - but then completely ignoring it!
您正在创建一个将使用 UTF-8 的编写器 - 但随后完全忽略了它!
Try this instead:
试试这个:
xStream.toXML(object, writer);
Also as a matter of style, I'd encourage you to consider the following:
另外,就风格而言,我鼓励您考虑以下几点:
- Don't compare results with Boolean constants; just use
if (foo)
orif (!foo)
instead - Catching
Exception
is veryrarely a good idea; catch specific exceptions instead - Returning a Boolean value to indicate success or failure isn't idiomatic Java; generally if something fails, an exception is better
- If the first
close
fails, you're quitting the method before the secondclose
call, which probably isn't what you want. (In fact, closing the OutputStreamWriter` will close the stream anyway, but consider the principle of the thing.) - Setting local variables to
null
at the end of a method is unnecessary and clutters up your code
- 不要将结果与布尔常量进行比较;只需使用
if (foo)
或if (!foo)
代替 - 捕
Exception
是非常罕见的好主意; 改为捕获特定异常 - 返回一个布尔值来指示成功或失败不是 Java 惯用的;通常,如果某事失败,则例外更好
- 如果第一次
close
失败,您将在第二次close
调用之前退出该方法,这可能不是您想要的。(其实,关闭OutputStreamWriter`无论如何都会关闭流,但要考虑事情的原理。) null
在方法的末尾设置局部变量是不必要的,并且会使您的代码变得混乱