Forum Discussion

evoks's avatar
evoks
Occasional Contributor
8 years ago

GroovyScript: Parse SOAP Response and get node names and node values

Hi all,   I have a SOAP response like this :      <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Header> <MessageID xmlns="http://www.w3.org">uuid: ...</MessageID> ...
  • rupert_anderson's avatar
    8 years ago

    Hi,

     

    Ok, no problem. Although I think you can surely assume that given that it's a SOAP response we can start at the Body element - would that be OK?

     

    If so, here is a recursive alternative:

     

    def results=[]
    
    def printNode
    printNode = { output,node ->         
        node.children().each{ printNode(results,it) }
        if (node.children().size()==0) output << node.name()+'='+node.text() //Exlude parent elements
    }
    
    slurperResponse.'**'.findAll { it.name() == 'Body' }.each { printNode(results,it) }
    
    results.each{
    	log.info it
    }

    This should:

    • Iterate over all child elements (not attributes, could add that)
    • Only print the child element names and values (not the parent ones, to get those remove the if statement that excludes them) 

    Is this what you need?

    Regards,

    Rup