xstream 正在转换 &到&从 xml 转换为 Java 对象时
从 xml 转换为 Java 对象时,xstream 正在将 &
转换为 &
。 我不希望这种事发生。我该怎么做呢?
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(HelpConfigVO.class);
xstream.processAnnotations(ProductVO.class);
xstream.processAnnotations(PackageVO.class);
xstream.addImplicitCollection(HelpConfigVO.class, "packages");
configVO = (HelpConfigVO)xstream.fromXML(helpConfigData);
其次,我从 xstream 获得的输出 XML 未格式化。如何格式化 xml 以使其可读?
xstream is converting &
to &
when converting from xml to Java Objects.
I don't want this to happen. How shall I do it?
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(HelpConfigVO.class);
xstream.processAnnotations(ProductVO.class);
xstream.processAnnotations(PackageVO.class);
xstream.addImplicitCollection(HelpConfigVO.class, "packages");
configVO = (HelpConfigVO)xstream.fromXML(helpConfigData);
Secondly, The output XML I am getting from xstream is not formatted. How do I format my xml to make it readable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
&
是一个XML实体。参阅 https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML
请 感觉 XStream 将其转义为
&
用于 Java 字符串。&
is a XML entity.See https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML
It makes sense that XStream unescapes it to
&
for a Java String.正如它应该的那样。如果没有的话就会坏掉
为什么? XML 中的
"&"
表示"&"
。没有有效的 XML 解析器会为您执行此操作,因为这是严重违反 XML 规范的行为。理论上,您可以破解开源 XML 解析器来执行这种令人憎恶的行为……但我强烈建议您不要这样做。
如果解析器中的正确行为给您带来了问题,则说明您的应用程序设计存在缺陷。您应该执行以下操作:
不解析 XML - 将其存储为字符序列,或者
通过应用 XML 转义映射重新插入字符实体在使用文本之前将其添加到文本中。
As it should. It would be broken if it didn't
Why? The
"&"
in the XML means"&"
.No valid XML parser will do this for you because it is an egregious violation of the XML spec. In theory, you could hack an open source XML parser to do this abominable act ... but I'd strongly advise against it.
If correct behavior in the parser is causing you problems, you've got a flaw in your application design. You should do something like:
not parsing the XML - store it as character sequence, OR
reinsert the character entities by applying the XML escape mapping to the text before you use it.
该软件的网站称“XStream 是一个简单的库,用于将对象序列化为 XML 并再次返回”。由于
&
是&
的 XML,因此不转换它是没有意义的,而且我非常怀疑它是否有“转换为文本,除非对于&符号,这将继续是XML”功能。听起来您遇到了XY 问题。为什么数据中需要
&
?假设这是因为您正在输出 XML 或 HTML,则适当的解决方案是将文本视为文本,并且:
The website for the software says "XStream is a simple library to serialize objects to XML and back again". Since
&
is XML for&
it wouldn't make sense for it not to convert it, and I doubt very much it has a "Convert to text except for ampersands which would continue to be XML" feature.It sounds like you have an XY problem. Why do you want
&
in the data?Assuming that it is because you are outputting XML or HTML, the appropriate solution is to treat the text as text and either:
你不能。
&
表示与; xstream 这样做是正确的。如果您确实希望输出为"&"
,则需要在代码中使用&&
,或者将其放入 CDATA 部分。You can't.
&
means ampersant; it's correct for xstream to do this. If you really want the output to be"&"
, you need&&
in your code, or put it inside a CDATA section.