Search code examples
javaxmlxml-parsingxstreamapostrophe

XStream apostrophe Issue in converting Java Object to XML


I am using com.thoughtworks.xstream.XStream to Generate xml String. I parse Object to xstream.toXML method and I get the xml output according to the way I need.

    <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);

Following is the Object structure.

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

So the element in the question, Friend's house is added to the List detailAnswer in Details class.

I get friend&apos;s House instead of friend's house. How can I resolve this. Is there special way to convert using XStream?


Solution

  • I think its better to use java method to replace a character.

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