xstream 正在转换 &到 &从 xml 转换为 Java 对象时 [英] xstream is converting & to & when converting from xml to Java Objects

查看:33
本文介绍了xstream 正在转换 &到 &从 xml 转换为 Java 对象时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xstream 在从 xml 转换为 Java 对象时将 & 转换为 &.我不希望这种情况发生.我该怎么做?

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);

其次,我从 xstream 获得的输出 XML 未格式化.如何格式化我的 xml 以使其可读?

Secondly, The output XML I am getting from xstream is not formatted. How do I format my xml to make it readable?

推荐答案

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

理所当然.不然就坏了

我不希望这种情况发生.

I don't want this to happen.

为什么?XML 中的 "&" 意味着 "&".

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

我该怎么做?

没有有效的 XML 解析器会为您执行此操作,因为它严重违反了 XML 规范.理论上,你可以破解一个开源 XML 解析器来做这种可恶的行为......但我强烈建议不要这样做.

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:

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

  • not parsing the XML - store it as character sequence, OR

在使用文本之前通过将 XML 转义映射应用于文本来重新插入字符实体.

reinsert the character entities by applying the XML escape mapping to the text before you use it.

这篇关于xstream 正在转换 &到 &从 xml 转换为 Java 对象时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆