Skip to content

Instantly share code, notes, and snippets.

@atbradley
Created March 11, 2012 00:15
Show Gist options
  • Save atbradley/2014184 to your computer and use it in GitHub Desktop.
Save atbradley/2014184 to your computer and use it in GitHub Desktop.
Return a String representation of an XmlParser Node.
/*
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