Skip to content

Instantly share code, notes, and snippets.

@devilelephant
Created September 13, 2013 18:48
Show Gist options
  • Select an option

  • Save devilelephant/6554529 to your computer and use it in GitHub Desktop.

Select an option

Save devilelephant/6554529 to your computer and use it in GitHub Desktop.
Groovy XML Markup Builder Tricks: CDATA, declaration, comments
def out = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(out)
// MarkupBuilder gives us an instance of MarkupBuilderHelper named 'mkp'
// MarkupBuilderHelper has several helpful methods
xml.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
xml.example {
a {
b {
mkp.comment('a comment')
c('normal elements')
}
}
d {
xml.mkp.yieldUnescaped("<![CDATA[Example of text in a CDATA block]]>")
}
}
assert out.toString() == '''
<?xml version='1.0' encoding='utf-8'?>
<example>
<a>
<b><!-- a comment -->
<c>normal elements</c>
</b>
</a>
<d><![CDATA[Example of text in a CDATA block]]></d>
</example>
'''.trim()
@rcr866

rcr866 commented Aug 25, 2014

Copy link
Copy Markdown

thanks!

@JasonArm

Copy link
Copy Markdown

Thanks.
(Bit more than a year ๐Ÿ˜‰ )

@Drezil

Drezil commented Nov 16, 2018

Copy link
Copy Markdown

Thanks.
(we can settle on 18 months as average .. ๐Ÿ˜‰ )

@gregorydickson

Copy link
Copy Markdown

Thanks

@slogic

slogic commented Dec 14, 2023

Copy link
Copy Markdown

Please, update example if "Example of text" also contains cdata section. Rigth now it is error prone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment