Forum Discussion

MKV's avatar
MKV
Contributor
6 years ago

iterate over a specific node in web service response using groovy

Hi,

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="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns4:GetAccountDetailsResponseMessage>

</ns4:account>

.....

<ns6:CustomCustomerAccount>

<ns3:attributes>
<ns3:attributeName>TELNUM</ns3:attributeName>
<ns3:attributeId>4232323</ns3:attributeId>
<ns3:attributeValue/>
</ns3:attributes>

<ns3:attributes>
<ns3:attributeName>SMSNUM</ns3:attributeName>
<ns3:attributeId>6565464</ns3:attributeId>
<ns3:attributeValue/>
</ns3:attributes>

</ns6:CustomCustomerAccount>
</ns4:account>
</ns4:GetAccountDetailsResponseMessage>
</soap:Body>
</soap:Envelope>

 

My Groovy scripting is as below:

import groovy.util.XmlSlurper
import com.eviware.soapui.support.XmlHolder

def WS_Data = context.expand( '${WS_GetAccountDetails#Response#declare namespace ns6.....GetAccountDetailsResponseMessage[1]/ns4:account[1]/ns6:CustomCustomerAccount[1]}' )

log.info("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
}
log.info (AttributesInfoList)

log.info("End")

 

Thanks!!

8 Replies

  • groovyguy's avatar
    groovyguy
    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
      MKV
      Contributor

      Hi,

       

      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
        groovyguy
        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!