2014-10-07 97 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