Raj Negi Raj Negi - 1 year ago 203
Groovy Question

Store multiple XML node values in string using Groovy

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


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 Source

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

//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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download