groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Keegan Witt <keeganw...@gmail.com>
Subject Re: How to convert a Node object to an XML String without escaping CDATA nodes value ?
Date Thu, 06 Aug 2015 02:00:22 GMT
Maybe I'm misunderstanding your use case, but why are you manually forcing
CDATA into the node text?  If you're parsing then reading node value, it
will automatically handle the CDATA.  Is this maybe what you meant to do?

def parent = new XmlParser().parseText('''
<parent>
    <child><![CDATA[you&me]]></child>
</parent>
''')
parent.append(new Node(parent, 'child2', parent.child.text()))
def sw = new StringWriter()
new XmlNodePrinter(new IndentPrinter(sw, '', false)).print(parent)
assert
"<parent><child>you&amp;me</child><child2>you&amp;me</child2><child2>you&amp;me</child2></parent>"
== sw.toString()

It won't write it with CDATA, but remember that <![CDATA[you&me]]> is
identical to you&amp;me for any parser following the spec.

-Keegan

On Wed, Aug 5, 2015 at 4:00 AM, Jean-Christophe Dominguez <jcd.pro@gmail.com
> wrote:

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

Mime
View raw message