Skip to content

Instantly share code, notes, and snippets.

@neelsmith
Last active December 28, 2015 18:49
Show Gist options
  • Save neelsmith/7546079 to your computer and use it in GitHub Desktop.
Save neelsmith/7546079 to your computer and use it in GitHub Desktop.
groovy script showing how to extract text node content from an XML file
/*
groovy XmlParser creates a tree of nodes that are
either Strings or groovy.util.Node objects. Walk the
parsed tree recursively and save text content.
*/
String collectText(Object n, String allText) {
if (n.getClass().getName() == "java.lang.String") {
allText = allText + n
} else {
n.children().each { child ->
allText = collectText(child, allText)
}
}
return allText
}
File f = new File(args[0])
def root = new XmlParser().parse(f)
println collectText(root, "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment