带有特殊字符的 Xstream
我正在使用 XStream,但我对特殊字符 á、é、í、ó、ú 和 ñ 遇到问题。
我尝试了这个:(
String charset = "UTF-8";
xstream = new XStream(new DomDriver(charset));
不起作用)
我的问题是我不知道如何提供 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)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是 就在javadoc中。
It's right there in the javadoc.
终于,它起作用了!
我修复了它,在 xml.getByte() 中添加“UTF-8”:
Finally, it's working !!!
I fix it adding "UTF-8" in xml.getByte():
事实上,我在这里感到有点困惑。
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.