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

时间:2014-10-07 08:40:25

标签: java xml xml-parsing xstream apostrophe

我正在使用 com.thoughtworks.xstream.XStream 来生成xml字符串。我将Object解析为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);

以下是Object结构。

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

因此问题中的元素朋友的房子被添加到详细信息类的List detailAnswer中。

我得到friend&apos;s House而不是friend's house。我该如何解决这个问题。是否有使用XStream进行转换的特殊方法?

1 个答案:

答案 0 :(得分:2)

我认为使用java方法替换字符会更好。

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