I want to be able to generate a CDATA portion in my XML using Groovy. I have used the mkp of the MarkupBuilder to do that, but it's not working. I am looping through products and then generate the following snippet as part of the XML. I get the following instead: It prints the unescaped text next to the product code instead of in the description, which is left blank.
<product>
<name>banana</name>
<code>10002</code><name>ICON_1</name><!CDATA[This product is on
sale]]]>
<description/>
</product>
Here is the portion which In use to generate the data.
product{
name (product.name)
code (product.code)
description mkp.yieldUnescaped("<!CDATA[${product.description}]]>")
}
This is what I want to print:
<product>
<name>banana</name>
<code>10002</code>
<description><![CDATA[This product is on sale]]></description>
</product>
You just need to use some curly braces around your CDATA
section to give the builder a hint of where to place it:
def out = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(out)
xml.product {
name ('banana')
code ('10002')
description { mkp.yieldUnescaped("<![CDATA[Example of text in a CDATA block]]>") }
}
System.out.println out.toString()
This produces:
<product>
<name>banana</name>
<code>10002</code>
<description><![CDATA[Example of text in a CDATA block]]></description>
</product>