Last active
December 28, 2015 18:49
-
-
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
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
/* | |
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