Forum Discussion

babusr01's avatar
babusr01
Contributor
8 years ago
Solved

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
    8 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
  • thanks for you time and efforts.

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    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?
    • babusr01's avatar
      babusr01
      Contributor

      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

      • nmrao's avatar
        nmrao
        Champion Level 3
        Will there be any other data in the excel file?
        Or just the records from the attached xml only?