Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/64a54aee57c11dc1a3f0 to your computer and use it in GitHub Desktop.
Save crazy4groovy/64a54aee57c11dc1a3f0 to your computer and use it in GitHub Desktop.
Generate a list of nodes in XML, and their frequency.
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