I’m using XmlParser to read in an XML document that has some content like this:
<instructions size='1'>
<instruction key='manifest'>
Bundle-SymbolicName: org.slf4j.api
Bundle-Version: 1.6.4.v20120130-2120
</instruction>
</instruction>
Notice on the 3rd line there is entity for newline, 

The problem is when I use Groovy’s XmlNodePrinter to print out this document (in my case I’ve made changes elsewhere to the document), the node printer will print out the text node and use a real newline instead of %#xA;
<instructions size='1'>
<instruction key='manifest'>
Bundle-SymbolicName: org.slf4j.api
Bundle-Version: 1.6.4.v20120130-2120
</instruction>
</instruction>
I have set the trimWhitespace=false for the XmlParser object and also set preserveWhitespace=true for XmlNodePrinter however this doesn’t change the aforementioned behavior.
You can wrap the text inside
<![CDATA...]]>:I’m testing it using the following the code:
The output is as expected:
RESPOND TO COMMENT:
If you really have no choice, than you can extends
XmlNodePrinter(this is a Java class) and create Groovy code such as:The output of this code is:
That
MyXmlNodePrinteris trivial and doesn’t perform escaping, so you may need to copy (and change)private void printEscaped(String s, boolean isAttributeValue)fromXmlNodePrinter. You can find the source code ofXmlNodePrinterin https://github.com/groovy/groovy-core/blob/master/subprojects/groovy-xml/src/main/java/groovy/util/XmlNodePrinter.java