2

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?

TV Nath
  • 499
  • 5
  • 12
  • 35
  • give your POJO code also. – Yogesh Prajapati Oct 07 '14 at 08:47
  • `'` means the same as `'` in XML, so it's not really _wrong_, just different. Try an alternative XStream driver such as `StaxDriver` and see if that helps matters. – Ian Roberts Oct 07 '14 at 09:18
  • yes, but the problem is when this String is saved as an xml file, I get ' instead of ' which is unacceptable. I have changes the XML structure names but it is actually for a business application – TV Nath Oct 07 '14 at 09:21
  • And when the receiver of the file parses it, their parser will turn `'` back into `'` transparently. Their code won't even know which representation was used in the original document. – Ian Roberts Oct 07 '14 at 09:38

1 Answers1

2

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

xStream.toXML(testPojo).replaceAll("&apos;", "'")
Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
  • 1
    Note that this risks breaking places where apostrophes _do_ need to be escaped as an entity reference, for example in attribute values that have been serialized with single quote delimiters. – Ian Roberts Oct 07 '14 at 15:03