Using PrettyPrintWriter to pretty print the xml file In the generated xml file the ' (apostrophe) is getting written as &apos Want it to print as '
Using the following xstream.marshal(obj, new PrettyPrintWriter(writer)) to pretty print ,any suggestions on how to print the escape characters as it is?
You can provide your own implementation of
PrettyPrintWriter, which extends that class and overrides itswriteText(QuickWriter, String)method.In its most basic form that would be something like this:
You would use this as follows:
The output file contains the following:
This is basic - it just passes the tag contents through to the file unchanged - nothing is escaped.
You are OK for content containing
",'and>. But this will be a problem for text containing>and&- which should still be escaped. So you can enhance yourwriteTextmethod to handle those cases as needed. See What characters do I need to escape in XML documents? for more details.Note also this is only for text values - not for XML attributes. There is a separate
writeAttributeValuemethod for that (probably not needed in your scenario).It is worth adding: There should be no need to do any of this. The XML is valid, with escaped values such as
'. Any process (any half-way decent XML library or tool) reading that data should handle them correctly.