Forum Discussion

babusr01's avatar
babusr01
Contributor
9 years ago

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
  • nmrao's avatar
    nmrao
    9 years ago

    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
  • babusr01's avatar
    babusr01
    9 years ago

    thanks for you time and efforts.