2

I have the following xml:

<list>         
<cars>   
<model>2012</model>
<make>GM</make>         
</cars>    
</list>

I want to print these values as path:value as shown below.

list/cars/model : 2012
list/cars/make : GM

How can I achieve this? I tried the name() method but it only prints the name of child item. I want to print the whole path till the element.

I can only use xmlSlurper parser to do this.

Thanks.

Bharath Reddy
  • 301
  • 2
  • 4
  • 15

2 Answers2

1
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, "")
Sachin
  • 1,208
  • 1
  • 10
  • 20
  • Thanks for the answer Sachin. – Bharath Reddy Jan 11 '16 at 04:53
  • What I am also looking for is.. runAssert(root/two, otherXmlRoot/Second) then the method is runAssert(x, y){ assert x.text() ==y.text() : failed [ x.name(), y.name()] } So, in this method for statement x.name(), y.name() I want to print the path. Sorry for the confusion. I appreciate your help. Thanks – Bharath Reddy Jan 11 '16 at 05:03
1
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

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • your answer seems interesting to me. can you explain, what are you trying to do? this might be helpful for others as well... btw your code is throwing exception: `groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChild.localText() is applicable for argument types: () values: []` – Sachin Jan 11 '16 at 07:51
  • I am trying to compare two elements from different xmls. Generally "assert x==y : msg" compares x &y and prints the msg if validation fails. So while printing the message I want to print the Complete path of x and y. The method I posted above takes these arguments and returns void. So we can add void for method signature. The problems is printing the complete path – Bharath Reddy Jan 11 '16 at 14:53
  • 1
    @BharathReddy *"The method I posted above takes these arguments and returns void."* I don't see any method in the question. – dmahapatro Jan 11 '16 at 15:33