Prabu
Prabu

Reputation: 3738

XmlSlurper parseText - node name replace with variable

Parsing the XML response, I'm declaring all the node's name in an array list myTag, and call the API endpoint and get the XML response text and stored in xmlResult.

then I looping all the node values and get the node text.

my code

def myTag =['appNo','date','name']

def xmlResult = new XmlSlurper().parseText(responseObject.getResponseText())

myTag .eachWithIndex({ def item, def index ->
    def readableTag = myTag.get(index)  
    def result = xmlResult.application.$readableTag.text()
    println result
})

I'm getting the empty result for all the nodes with the above looping, but when I use the below I'm getting the result.

def result = xmlResult.application.appNo.text()
def result1 = xmlResult.application.date.text()
def result2 = xmlResult.application.name.text()

Let me know how can I pass the node value from variable and how can I replace with the variable value in the above statement?

Upvotes: 1

Views: 292

Answers (1)

Prabu
Prabu

Reputation: 3738

I got the answer, passing the variable within the double quote.

myTag .eachWithIndex({ def item, def index ->
    def readableTag = myTag.get(index)  
    def result = xmlResult.application."${readableTag}".text()
    println result
})

Upvotes: 1

Related Questions