带有特殊字符的 Xstream

发布于 2024-10-21 12:51:01 字数 950 浏览 2 评论 0原文

我正在使用 XStream,但我对特殊字符 á、é、í、ó、ú 和 ñ 遇到问题。

我尝试了这个:(

  String charset = "UTF-8";
  xstream = new XStream(new DomDriver(charset));

不起作用)

我发现 XStream 本身不进行字符编码,它依赖于关于底层 XML 编写器的配置。默认情况下,它使用自己的 PrettyPrintWriter 写入当前语言环境的默认编码。要编写 UTF-8,您必须自己提供具有适当编码的 Writer。

我的问题是我不知道如何提供 Writer...

// get the model from the map passed created by the controller
Object model = map.get("model");

Object viewData = transformViewData(model);

//TEST
Writer w = new OutputStreamWriter(viewData, "UTF-8");
//FINTEST

// if the model is null, we have an exception
String xml = null;
if (viewData != null){
    xml = xstream.toXML(viewData, w);  //Err:Cannot find symbol...
}else{
    // so render entire map
    xml = xstream.toXML(map, w); //Err:Cannot find symbol...
}

response.getOutputStream().write(xml.getBytes());

I'm working with XStream but I have a problem with the special characters á,é,í,ó,ú and ñ.

I tried this:

  String charset = "UTF-8";
  xstream = new XStream(new DomDriver(charset));

(don't work)

I found that XStream does no character encoding by itself, it relies on the configuration of the underlying XML writer. By default it uses its own PrettyPrintWriter which writes into the default encoding of the current locale. To write UTF-8 you have to provide a Writer with the appropriate encoding yourself.

My problem is that I don't know how to provide a Writer...

// get the model from the map passed created by the controller
Object model = map.get("model");

Object viewData = transformViewData(model);

//TEST
Writer w = new OutputStreamWriter(viewData, "UTF-8");
//FINTEST

// if the model is null, we have an exception
String xml = null;
if (viewData != null){
    xml = xstream.toXML(viewData, w);  //Err:Cannot find symbol...
}else{
    // so render entire map
    xml = xstream.toXML(map, w); //Err:Cannot find symbol...
}

response.getOutputStream().write(xml.getBytes());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风情万种。 2024-10-28 12:51:01

它是 就在javadoc中

Writer w = new OutputStreamWriter(new FileOutputStream("test.xml"), "UTF-8");
XStream.toXML(object, w);

It's right there in the javadoc.

Writer w = new OutputStreamWriter(new FileOutputStream("test.xml"), "UTF-8");
XStream.toXML(object, w);
羅雙樹 2024-10-28 12:51:01

终于,它起作用了!

我修复了它,在 xml.getByte() 中添加“UTF-8”:

response.getOutputStream().write(xml.getBytes("UTF-8"));

Finally, it's working !!!

I fix it adding "UTF-8" in xml.getByte():

response.getOutputStream().write(xml.getBytes("UTF-8"));
七七 2024-10-28 12:51:01

事实上,我在这里感到有点困惑。

Xstream 做得很好,让我解释一下原因。

当您在文本编辑器中打开 xml 时,“特殊字符”会变得混乱;事实上,您一开始就不应该在文本编辑器中打开 xml!

请记住,Xml 和 Html 是孪生语言(前者用于携带数据,后者用于显示数据),并且就像 html 文件一样,xml 文件也应该由 Web 浏览器打开。

因此,Xstream 替换了字符串中的“特殊字符”,以便您可以在 Web 浏览器中正确读取它。

Actually, i sense a little bit of confusion here.

Xstream is doing the job just fine, let me explain why.

"Special characters" are messed up when you open the xml in a text editor; the fact is that you shouldn't open the xml in a text editor in the first place!

Keep in mind that Xml and Html are twinned languanges(the first intended to carry data, the latter intended to display data), and just like html files also xml files are supposed to be opened by a web browser.

So Xstream replaces the "special characters" in the string so that you can correctly read it in a web browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文