groovy Print path and value of elements in xml

只谈情不闲聊 提交于 2019-12-06 04:21:21
import groovy.util.XmlSlurper
import groovy.util.slurpersupport.NodeChild;
def rootNode = new XmlSlurper().parseText('<root><one a1="uno!"/><two>Some text!</two></root>' )

def printMap 
printMap = {node, path->
     if(node.getClass() == NodeChild){
        node.childNodes().each{
           printMap(it, (path ? path + "/" : "") + node.name())
        }
     } else {
        println "${path}/${node.name()}:${node.text()}"
     }
 }
 printMap(rootNode, "")
def xml = '''
<list>         
    <cars>   
        <model>2012</model>
        <make>GM</make>
        <color>Gold</color>
    </cars>    
</list>
'''

def item = new XmlSlurper().parseText(xml)

item.'**'.inject([]) { acc, val ->
    def localText = val.localText()
    acc << val.name()

    if( localText ) {
        println "${acc.join('/')} : ${localText.join(',')}"
        acc = acc.dropRight(1) // or acc = acc[0..-2]
    } 
    acc
}

This would print as required. Above is using a depthFirst() search on the tree and using inject (just not to mutate any other list) and looks for localText(). If localText() is encountered indicating the value of the leaf node, then print the path and the value. The path has been accumulated in a list which was used in inject. A simple join() would give the required format.

Above has been tested successfully in Groovy 2.4.5. If localText() is not available in NodeChild then a version of Groovy older than 2.3.0 would be reason because that method has been added since 2.3.0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!