Created
September 13, 2013 18:48
-
-
Save devilelephant/6554529 to your computer and use it in GitHub Desktop.
Groovy XML Markup Builder Tricks: CDATA, declaration, comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
Thanks