Last active
August 29, 2015 14:03
-
-
Save crazy4groovy/64a54aee57c11dc1a3f0 to your computer and use it in GitHub Desktop.
Generate a list of nodes in XML, and their frequency.
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
File f = new File(/C:\data.xml/) | |
int ratio = 10 | |
String delim = '/' | |
def root = new XmlSlurper() | |
root.setFeature('http://apache.org/xml/features/disallow-doctype-decl', false) // allow XML doctype tag to exist | |
root = root.parse(f) | |
String rootName = root.name() | |
Map nodeCount = [:].withDefault{0} | |
root.'**'.each {n -> | |
String name = "" | |
while(n.name() != rootName) { | |
name = delim + n.name() + name | |
n = n.parent() | |
} | |
if (name) | |
nodeCount[name]++ | |
} | |
println "---\n${nodeCount.collect{it}.join('\n')}" | |
/***find the most common nodes***/ | |
int maxCount = nodeCount.collect{k,v -> v}.max().intdiv(ratio) | |
println "---\n filterBy: count=${maxCount}" | |
println "---\n${nodeCount.findAll{k,v -> (v > maxCount)}.collect{it}.join('\n')}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment