Created
March 11, 2012 00:15
-
-
Save atbradley/2014184 to your computer and use it in GitHub Desktop.
Return a String representation of an XmlParser Node.
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
/* | |
Similar to XSLT's copy-of element. Useful if e.g. you have node containing XHTML | |
that you want to return as-is. | |
*/ | |
Node.metaClass.asString = { | |
def text = [] | |
delegate.children().each { child -> | |
if ( child instanceof String ) { | |
text.add child | |
} else { | |
def attrs = [] | |
child.attributes().each { key, val -> | |
attrs.add "${key}=\"${val}\"" | |
} | |
attrs = attrs ? ' '+attrs.join(' ') : '' | |
text.add "<${child.name()}${attrs}>${child.text()}<${child.name()}>" | |
} | |
} | |
text.join(' ') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment