xstream 正在转换 &到&从 xml 转换为 Java 对象时

发布于 2024-12-27 22:57:15 字数 548 浏览 0 评论 0原文

从 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 技术交流群。

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

发布评论

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

评论(4

无法回应 2025-01-03 22:57:16

&是一个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.

盛夏已如深秋| 2025-01-03 22:57:15

从 xml 转换为 Java 对象时,xstream 正在将 "&" 转换为 "&"

正如它应该的那样。如果没有的话就会坏掉

我不希望这种事发生。

为什么? XML 中的"&"表示"&"

我该怎么做?

没有有效的 XML 解析器会为您执行此操作,因为这是严重违反 XML 规范的行为。理论上,您可以破解开源 XML 解析器来执行这种令人憎恶的行为……但我强烈建议您不要这样做。


如果解析器中的正确行为给您带来了问题,则说明您的应用程序设计存在缺陷。您应该执行以下操作:

  • 不解析 XML - 将其存储为字符序列,或者

  • 通过应用 XML 转义映射重新插入字符实体在使用文本之前将其添加到文本中。

xstream is converting "&" to "&" when converting from xml to Java Objects.

As it should. It would be broken if it didn't

I don't want this to happen.

Why? The "&" in the XML means "&".

How shall I do it?

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.

等你爱我 2025-01-03 22:57:15

该软件的网站称“XStream 是一个简单的库,用于将对象序列化为 XML 并再次返回”。由于 && 的 XML,因此不转换它是没有意义的,而且我非常怀疑它是否有“转换为文本,除非对于&符号,这将继续是XML”功能。

听起来您遇到了XY 问题。为什么数据中需要 &

假设这是因为您正在输出 XML 或 HTML,则适当的解决方案是将文本视为文本,并且:

  • 使用您用来构建 XML/HTML 的任何库的“插入文本”功能(而不是“插入标记”功能)
  • 通过转义函数运行文本(例如 Perl 的 HTML::Entities 模块或 PHP 的 htmlspecialchars 函数(我对 Java 的使用不够为该语言推荐一个,但原理是相同的))。

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:

  • Use the "insert text" feature of whatever library you are using to build the XML/HTML (as opposed to the "insert markup" feature)
  • Run the text though an escaping function (such as Perl's HTML::Entities module or PHP's htmlspecialchars function (I don't use Java enough to recommend one for that language, but the principle is the same)).
海螺姑娘 2025-01-03 22:57:15

你不能。 & 表示与; 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.

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