Forum Discussion

MKV's avatar
6 years ago

iterate over a specific node in web service response using groovy


I want to iterate over a specific node in web service response and store the data in an array. I tried to do some groovy scripting referring the samples in the forum. But the data doesn't get added to the array. I'm trying to loop over the attributeName node in the below sample. Kindly help to resole the issue.


My response is like this:

<soap:Envelope xmlns:soap="">








My Groovy scripting is as below:

import groovy.util.XmlSlurper

def WS_Data = context.expand( '${WS_GetAccountDetails#Response#declare namespace ns6.....GetAccountDetailsResponseMessage[1]/ns4:account[1]/ns6:CustomCustomerAccount[1]}' )"Start")
class AttributesInfo {
def attributeNameObj
List<AttributesInfo> AttributesInfoList = new ArrayList<>()
def WS_Results = new XmlSlurper().parseText(WS_Data)
def WS_attributes = WS_Results.Body.GetAccountDetailsResponseMessage.account.CustomCustomerAccount.attributes

// iterate over attributes data
WS_attributes.each { attributes ->
AttributesInfo attributeInfo = new AttributesInfo()
attributeInfo.attributeNameObj = attributes.attributeName
AttributesInfoList << attributeInfo
} (AttributesInfoList)"End")



8 Replies

  • groovyguy's avatar
    Community Hero

    Am I corect in my assumption that you are trying to parse one XML object and all of its elements to use them in future requests? Groovy will work, but it may take a lot of different steps. You might want to look at using an XML data source. That might better fit what you want. You can set it to parse an XML response and use XPATH to select elements and load them into properties. 

    • MKV's avatar



      I tried to use it as XML Datasource. I created the properties and executed it. But the data log doesn't seem to be displaying any data although it says Got 5 rows in 3ms.


      1. Is there something that needs to be done, apart from providing the row Xpath, defining property?

      2. Can i use the output data values to get stored in an array?


      Please suggest.

      • groovyguy's avatar
        Community Hero

        Can you share some details of what you have tried? Either your project or screen shots? 

        What I have done in the past is similar to this, as a high level view:

        1. Make a Read request that returns one object.
        2. Set up an XML datasource that uses XPATH to point to properties in the response from step 1.
          1. Example properties:
            1. UserName
            2. TimeStamp
            3. Identifier
          2. If you manually run the DataSource, you should see the data it returns in the data log window. If you do not see data, it is not configured correctly.
        3. Set up furthe Read requests that use data from the XML Data Source
          1. Test steps would reference the data as ${XMLDataSource#PropertyName}
            1. ${XMLDataSource#UserName}
            2. ${XMLDataSource#TimeStamp}
            3. ${XMLDataSource#Identifier}
        4. An example test case would look like
          1. Read request to populate XML DataSource
          2. XML DataSource
          3. ReadRequest using Property1
          4. ReadRequest using Property2
          5. ReadRequest using Property3
          6. etc.


        Hope that helps!