How to parse a webservice response with Groovy

Hi There,

I’ve got some trouble in parsing my webservice response to remove all xml lines which include i:nil=“true”.
I have to do it because Katalon is not able to verify a text element with this script :

def response = WS.sendRequest(findTestObject(‘API’))

_if (WS.containsString(response, ‘:nil=“true”’, false)) _

println “The return API contain ‘:nil=true’”

WS.verifyElementText(response, ‘Response.Data.NextDeparture[0].Destination’, ‘Debourg’)

Here is my webservice response :

_ _

_ _

_ Debourg_

_ 1094_

_ 0_

_ _

_ _

_ _

_ Debourg_

_ 1099_

_ 0_

_ _

_ _

_ _

_ Debourg_

_ 1104_

_ 0_

_ _

_ _

_ _

_ _

_ 200_

Here is the log :

Unable to verify element text (Root cause: org.xml.sax.SAXParseException: The prefix “i” for attribute “i:nil” associated with an element type “JourneyActivityCode” is not bound.)

Does anyone of you already did it ? Could you help me please ?
Thank you very much !!

Best regards

1 Like

import groovy.xml.MarkupBuilder

import groovy.util.*RequestObject reqObj = findTestObject(‘API_request’)

responseData = WS.sendRequest(reqObj)

responseText = responseData.getResponseText()

def parser = new XmlParser()

def doc = parser.parseText(responseText);

doc.NextDeparture.each {

dp - > print("Destination:") println "${dp.Destination\[0\].text}"

print("Hour:") println "${dp.Hour\[0\].text}"

print("Hour type:") println "${dp.HourType\[0\].text}"

print("JourneyActivityCode:") println "${dp.JourneyActivityCode\[0\]\[@ i:nil\]}"

}

i don’t know it will work or not

2 Likes