cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy iterate Nodes with XMLHolder response -soapui

SOLVED
Highlighted
Contributor

Groovy iterate Nodes with XMLHolder response -soapui

Hi ,

 

Could some one help me read the Data from & write XML Response?

I am able to read single node response and write in Excel sheet. But i need to iterate. 

 

Thanks

Babu

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

Re: Groovy iterate Nodes with XMLHolder response -soapui

Here is the script which reads your data and put it into list of StateInfo objects. All you need to do is loop thru the list of stateInfos and put it into excel. Hope you should be able to do it.

 

import groovy.xml.*
//Business Model class
class StateInfo {
  def city
  def state
  def zip
  def areaCode
  def timeZone
}

def xmlString = '''<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetInfoByStateResponse xmlns="http://www.webserviceX.NET">
         <GetInfoByStateResult>
            <NewDataSet xmlns="">
               <Table>
                  <CITY>Wharton</CITY>
                  <STATE>TX</STATE>
                  <ZIP>77488</ZIP>
                  <AREA_CODE>409</AREA_CODE>
                  <TIME_ZONE>C</TIME_ZONE>
               </Table>
               <Table>
                  <CITY>Wheeler</CITY>
                  <STATE>TX</STATE>
                  <ZIP>79096</ZIP>
                  <AREA_CODE>806</AREA_CODE>
                  <TIME_ZONE>C</TIME_ZONE>
               </Table>
               <Table>
                  <CITY>Wheelock</CITY>
                  <STATE>TX</STATE>
                  <ZIP>77882</ZIP>
                  <AREA_CODE>409</AREA_CODE>
                  <TIME_ZONE>C</TIME_ZONE>
               </Table>
            </NewDataSet>
         </GetInfoByStateResult>
      </GetInfoByStateResponse>
   </soap:Body>
</soap:Envelope>'''

//println xmlString
List<StateInfo> stateInfos = new ArrayList<>()
def slurper = new XmlSlurper().parseText(xmlString)
def tables = slurper.Body.GetInfoByStateResponse.GetInfoByStateResult.NewDataSet.Table
tables.each { table ->
  StateInfo stateInfo = new StateInfo()
  stateInfo.city = table.CITY
  stateInfo.state = table.STATE
  stateInfo.zip = table.ZIP
  stateInfo.areaCode = table.AREA_CODE
  stateInfo.timeZone = table.TIME_ZONE
  stateInfos << stateInfo
}

assert stateInfos.size() == 3


Regards,
Rao.

View solution in original post

Highlighted
Contributor

Re: Groovy iterate Nodes with XMLHolder response -soapui

thanks for you time and efforts.

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

Re: Groovy iterate Nodes with XMLHolder response -soapui

What is the expected output format that you need to write?
In the txt file that you attached have 3 records, so you need 3 rows?


Regards,
Rao.
Highlighted
Contributor

Re: Groovy iterate Nodes with XMLHolder response -soapui

Yes,

 

The attached file is response from previous step and next step is groovy to read and write in Excel sheet.

I am using Xpath and able to write when one record, but i need to write all 3 records.

 

Thanks

Babu

Highlighted
Community Hero

Re: Groovy iterate Nodes with XMLHolder response -soapui

Will there be any other data in the excel file?
Or just the records from the attached xml only?


Regards,
Rao.
Highlighted
Contributor

Re: Groovy iterate Nodes with XMLHolder response -soapui

Thanks for Quick response.

I have idea and code to read and write excel file, But I need to read response which i attached.

 

Thanks 

Babu

 

Highlighted
Community Hero

Re: Groovy iterate Nodes with XMLHolder response -soapui

Here is the script which reads your data and put it into list of StateInfo objects. All you need to do is loop thru the list of stateInfos and put it into excel. Hope you should be able to do it.

 

import groovy.xml.*
//Business Model class
class StateInfo {
  def city
  def state
  def zip
  def areaCode
  def timeZone
}

def xmlString = '''<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetInfoByStateResponse xmlns="http://www.webserviceX.NET">
         <GetInfoByStateResult>
            <NewDataSet xmlns="">
               <Table>
                  <CITY>Wharton</CITY>
                  <STATE>TX</STATE>
                  <ZIP>77488</ZIP>
                  <AREA_CODE>409</AREA_CODE>
                  <TIME_ZONE>C</TIME_ZONE>
               </Table>
               <Table>
                  <CITY>Wheeler</CITY>
                  <STATE>TX</STATE>
                  <ZIP>79096</ZIP>
                  <AREA_CODE>806</AREA_CODE>
                  <TIME_ZONE>C</TIME_ZONE>
               </Table>
               <Table>
                  <CITY>Wheelock</CITY>
                  <STATE>TX</STATE>
                  <ZIP>77882</ZIP>
                  <AREA_CODE>409</AREA_CODE>
                  <TIME_ZONE>C</TIME_ZONE>
               </Table>
            </NewDataSet>
         </GetInfoByStateResult>
      </GetInfoByStateResponse>
   </soap:Body>
</soap:Envelope>'''

//println xmlString
List<StateInfo> stateInfos = new ArrayList<>()
def slurper = new XmlSlurper().parseText(xmlString)
def tables = slurper.Body.GetInfoByStateResponse.GetInfoByStateResult.NewDataSet.Table
tables.each { table ->
  StateInfo stateInfo = new StateInfo()
  stateInfo.city = table.CITY
  stateInfo.state = table.STATE
  stateInfo.zip = table.ZIP
  stateInfo.areaCode = table.AREA_CODE
  stateInfo.timeZone = table.TIME_ZONE
  stateInfos << stateInfo
}

assert stateInfos.size() == 3


Regards,
Rao.

View solution in original post

Highlighted
Contributor

Re: Groovy iterate Nodes with XMLHolder response -soapui

thanks for you time and efforts.

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors