2014-10-07 98 views
2

我正在使用com.thoughtworks.xstream.XStream来生成XML字符串。我将Object解析为xstream。 toXML方法,并根据我需要的方式得到xml输出。XStream撇号将Java对象转换为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 

所以在问题的元素,朋友的房子被添加到列表中detailAnswer详细类。

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

+0

也给你的POJO代码。 – 2014-10-07 08:47:15

+0

更新了问题 – 2014-10-07 08:59:47

+0

'''与XML中的'''意思相同,所以它不是真的_wrong_,只是不同而已。尝试一个替代的XStream驱动程序,比如'StaxDriver',看看它是否有帮助。 – 2014-10-07 09:18:14

回答

2

我觉得最好用java方法来替换一个字符。

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

请注意,这可能会导致撇号_do_需要作为实体引用进行转义的地方,例如在使用单引号分隔符序列化的属性值中。 – 2014-10-07 15:03:58