我正在使用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'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("'", "'")
关于java - 将 Java 对象转换为 XML 时的 XStream 撇号问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26231954/