Last active
August 29, 2015 14:07
-
-
Save playerjamesbattleground/0126eb401989f8f54410 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import groovy.util.XmlSlurper; | |
import org.apache.commons.lang3.StringUtils; | |
class Xml2MapHelper { | |
def log = org.slf4j.LoggerFactory.getLogger(getClass()) | |
/* | |
* these functions only return list of maps which contains the bottom level key-value pairs, | |
* ie. convert whatever input xml into a key:list<map> structure | |
*/ | |
def toMap(node) { | |
// base case: | |
if(node.children().isEmpty()) { | |
return node.text() | |
} | |
else { // we have nested elements | |
return node.children().inject([:], { map, c -> | |
addValue(c.name(), toMap(c), map) | |
}) | |
} | |
} | |
def addValue(name, value, map) { | |
if(map[name]) { | |
if(map[name] instanceof List) { // already multi-valued | |
map[name] << value | |
} else { | |
map[name] = [map[name], value] // single value, need to make multi-valued | |
} | |
} else { | |
map[name] = value // no value yet | |
} | |
return map | |
} | |
slurper = new XmlSlurper() | |
xmlDoc = slurper.parseText(xmlText) | |
documentMap = toMap(xmlDoc) | |
println documentMap | |
/* | |
* This only drills down to given level, and MUST have known the lowest level are key-value pai | |
*/ | |
// def mapXml (root,depth) { | |
// XmlSlurper xs = new XmlSlurper(); | |
// if(org.apache.commons.lang3.StringUtils.isEmpty(root)){ | |
// return null | |
// } | |
// def parsed = xs.parseText(root) | |
// | |
// def varray = [] | |
// | |
// if(depth == 0) { | |
// def nodemap = [:] | |
// parsed.each{ele-> | |
// nodemap[ele.name()]=ele.text() | |
// } | |
// varray << nodemap | |
// }else { | |
// | |
// xml2fixdepthmap(parsed, varray, depth) | |
// } | |
// | |
// if(varray.size() == 0) { | |
// return null | |
// } | |
// return varray | |
// } | |
// | |
// | |
// def xml2fixdepthmap(parsed, array, depth) { | |
// if(depth-1 < 0) { | |
// def attrmap=[:] | |
// parsed.childNodes().each{ elem-> | |
// attrmap[elem.name()]=elem.text() | |
// } | |
// array << attrmap | |
// return array | |
// } | |
// | |
// depth = (depth-1) | |
// | |
// | |
// def dig = false | |
// parsed.childNodes().each{it -> | |
// dig = it.childNodes()? true : false | |
// def tmp = [:] | |
// tmp[it.name()]=it.childNodes() ? xml2fixdepthmap(it,[],depth) : it.text() | |
// array << tmp | |
// } | |
// | |
// return array | |
// } | |
// /* | |
// * Not in use: This is for when lowest level are plain texts | |
// */ | |
// def xml2map (root) { | |
// XmlSlurper xs = new XmlSlurper(); | |
// if(org.apache.commons.lang3.StringUtils.isEmpty(root)){ | |
// return null | |
// } | |
// def parsed = xs.parseText(root) | |
// | |
// def varray = xml2json(parsed) | |
// if(varray.size() == 0) { | |
// return null | |
// } | |
// return varray | |
// } | |
// | |
// def xml2json (parsed) { | |
// | |
// def ary = parsed.childNodes().collect{ elem -> | |
// | |
// [ (elem.name()) : (elem.childNodes()? xml2json(elem) : elem.text().trim())] | |
// | |
// } | |
// return ary | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment