groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jean-Christophe Dominguez <jcd....@gmail.com>
Subject How to convert a Node object to an XML String without escaping CDATA nodes value ?
Date Wed, 05 Aug 2015 08:00:32 GMT
Hi

I'm using groovy.util.Node to manipulate tree structures and than I store
the trees in XML format.

I need some nodes value not to be escaped.

Can you help me ?

Here's the code :

def nodeValue = '<![CDATA[you&me]]>'
Node rootNode = new Node(null, 'parent', [:], nodeValue)
rootNode.append(new Node(null, 'child', [:], nodeValue))

println groovy.xml.XmlUtil.serialize(rootNode) // & escaped to &amp;

def sw = new StringWriter()
new XmlNodePrinter(new IndentPrinter(sw, '', false)).print(rootNode)
println sw.toString()  // & escaped to &amp;

 And the output:

 <?xml version="1.0" encoding="UTF-8"?><parent>
&lt;![CDATA[you&amp;me]]&gt;  <child>&lt;![CDATA[you&amp;me]]&gt;</child>
</parent>

<parent>&lt;![CDATA[you&amp;me]]&gt;<child>&lt;![CDATA[you&amp;me]]&gt;</child></parent>

Thanks,

---
Jean-Christophe
<http://www.crealead.com>

Mime
View raw message