将 Java 对象转换为 XML 时的 XStream 撇号问题

2024-01-01

我在用com.thoughtworks.xstream.XStream生成 xml 字符串。我将对象解析为 xstream。toXML方法,我根据我需要的方式获得 xml 输出。

    <myxml>
      <test type="test" name="test">
        <question id="Name" answer="Micheal"/>
        <question id="Address" answer="Home">
          <details name="First Address">
            <detailanswer>friend&apos;s House</detailanswer>
          </details>
        </basequestion>
      </test>
    </myxml>

XStream xstream = new XStream();
xstream.alias("myxml", MyXml.class);
xstream.alias("test", Test.class);
xstream.alias("question", Question.class);
xstream.alias("details", Details.class);
xstream.alias("detailanswer", String.class);
xstream.addImplicitCollection(MyXml.class, "test");
xstream.addImplicitCollection(Test.class, "question");
xstream.addImplicitCollection(Question.class, "details");
xstream.addImplicitCollection(Details.class, "detailanswer");

xstream.useAttributeFor(Test.class, "type");
xstream.useAttributeFor(Test.class, "name");

xstream.useAttributeFor(Question.class, "id");
xstream.useAttributeFor(Question.class, "answer");
xstream.useAttributeFor(Details.class, "name");

return xstream.toXML(eform);

以下是对象结构。

Inside MyXml there is List<Test>
Test has List<Question>, String type, String name
Question has List<Details>, String id, String answer.
Details has List<String> detailAnswer, String name

所以问题中的要素,朋友的家添加到Details 类中的列表detailAnswer 中。

I get friend&apos;s House代替friend's house。我该如何解决这个问题。有没有特殊的方法使用XStream进行转换?


我觉得用java的方法来替换字符比较好。

xStream.toXML(testPojo).replaceAll("&apos;", "'")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 Java 对象转换为 XML 时的 XStream 撇号问题 的相关文章

随机推荐