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

标签 java xml xml-parsing xstream apostrophe

我正在使用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

因此,问题中的元素 Friend's house 被添加到 Details 类中的 ListDetailAnswer 中。

我得到 friend 的房子而不是 friend 的房子。我该如何解决这个问题。有没有特殊的方法使用XStream进行转换?

最佳答案

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

xStream.toXML(testPojo).replaceAll("&apos;", "'")

关于java - 将 Java 对象转换为 XML 时的 XStream 撇号问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26231954/

相关文章:

java - Lambdas : local variables need final, 实例变量没有

java - java中根据条件使用流操作将List转换为Map

java - 如何将来自 SAX 解析器的 XML 数据存储在 java 对象中?

java - 从 xml 解析文本

java - 使用子元素的 JAXB 注释

c++ - 编码进度条建议

java - 通过按钮调用时不显示用户输入。使用 Java 数组列表

java - 无法在java中将大文件(>1gb)转换为字节数组

xml - 在 Linux 上,将 XML 解析为 MYSQL 的最有效方法是什么?

java - 使用 XOM 解析 XML 时出现空子元素