Raj Negi Raj Negi - 26 days ago 7
Groovy Question

Store multiple XML node values in string using Groovy

I am trying to store XML response node values to a string.
Response :

<ReturnResourceItemResponse>
<ReturnResourceItemResult>
<countryID>1</countryID>
<resourceItemID>2000</resourceItemID>
<locationID>88</locationID>
<groupID>518</groupID>
<floor>1</floor>
<resourceID>1</resourceID>
<name>Hello</name>
</ReturnResourceItemResult>
</ReturnResourceItemResponse>


Result i want :

[2000, Hello, 1, 1, 88, 518, 1]


What i tried :

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(messageExchange.responseContent)
def nodes = holder.getDomNodes( "//*:countryID" )
def XMLlist = []
for( node in nodes )
{
def value = com.eviware.soapui.support.xml.XmlUtils.getNodeValue( node )
XMLlist.add( value)
}
log.info "XML_List =" + XMLlist


Result i am getting :

XML_List =[1]

Rao Rao
Answer

It is noticed that OP is using Script Assertion for the request step.

Here is the Script Assertion which asserts the response and retrieves the data that OP requested.

Script Assertion

//Retrieves the ReturnResourceItemResult
def getItemResult = { response ->
   def parsedResponse = new XmlSlurper().parseText(response)
   parsedResponse.'**'.find{it.name() == 'ReturnResourceItemResult' }
}

//Retrieves the data from Item result
def getItemResultData = { itemResult, elements ->
   def result = []
   elements.each { element ->
    result << itemResult.'**'.find{it.name() == element} as String
   }
   result
}

//Assert if result is not empty or null
assert context.response, "Response data is empty or null"

//Defining the element order will have the better control on result order
def list = ['resourceItemID', 'name', 'countryID', 'resourceID', 'locationID', 'groupID', 'floor']

//Jus call the above closures to get the data
def data = getItemResultData(getItemResult(context.response), list)
//Show the result
log.info "Extracted data is :${data}"

You may quickly test it from here Demo

Note that, had to make the xml well-formed, so changed a bit.

Hope this is helpful.