首页 文章

使用SOAPUI中的Groovy脚本获取文本并存储到Excel工作表中

提问于
浏览
0

我需要从soapui工具中获取xml的值并将这些数据存储到Excel工作表中 . 我在SoapUI工具中使用了groovy脚本 .

如果Response有多个输出,这些输出存储在excel表中 . 就像LocationName和CustCity333Name有两次,所以这些输出应该存储到excel表中 . 请帮我解决这个问题

<ns10:Location>
           <ns10:LocationId>
              <ns5:RowId>7080013</ns5:RowId>
           </ns10:LocationId>
           <ns10:LocationDetails>
              <ns10:AuditElement/>
              <ns10:EffectiveDate/>
              **<ns10:LocationName>REMOVEDEPENDENCY004</ns10:LocationName>**
             <ns10:RailIncData/>
              **<ns10:CustCity333Name>OAKLAND</ns10:CustCity333Name>**
              <ns10:CustCity333Id>OAKLAND</ns10:CustCity333Id>
              <ns10:CustCity333StateCode>TN</ns10:CustCity333StateCode>
              <ns10:ParentCIFDetails/>
           </ns10:LocationDetails>
        </ns10:Location>
        <ns10:Location>
           <ns10:LocationId>
              <ns5:RowId>7080018</ns5:RowId>
           </ns10:LocationId>
           <ns10:LocationDetails>
              <ns10:AuditElement/>
              <ns10:EffectiveDate/>
              **<ns10:LocationName>REMOVEDEPENDENCY004</ns10:LocationName>**
              <ns10:RailIncData/>
              **<ns10:CustCity333Name>OAKLAND</ns10:CustCity333Name>**
              <ns10:CustCity333Id>OAKLAND</ns10:CustCity333Id>
              <ns10:CustCity333StateCode>TN</ns10:CustCity333StateCode>
              <ns10:ParentCIFDetails/>
           </ns10:LocationDetails>

1 回答

  • 0

    注意:

    • 由于您提到excel或csv没问题,下面的脚本使用 csv 格式 .

    • 您提供的数据格式不正确 . 修改了一下以使其格式良好 .

    • 还使用 rowId 进行行中的识别 . 如果您不希望,可以删除 .

    Groovy Script

    //Provide / edit file path  for csv  file in the below
    def fileName = '/tmp/locationData.csv'
    
    def xml = """<root xmlns:ns10='url1' xmlns:ns5='url2'> <ns10:Location>
               <ns10:LocationId>
                  <ns5:RowId>7080013</ns5:RowId>
               </ns10:LocationId>
               <ns10:LocationDetails>
                  <ns10:AuditElement/>
                  <ns10:EffectiveDate/>
                  <ns10:LocationName>REMOVEDEPENDENCY004</ns10:LocationName>**
                 <ns10:RailIncData/>
                  <ns10:CustCity333Name>OAKLAND</ns10:CustCity333Name>**
                  <ns10:CustCity333Id>OAKLAND</ns10:CustCity333Id>
                  <ns10:CustCity333StateCode>TN</ns10:CustCity333StateCode>
                  <ns10:ParentCIFDetails/>
               </ns10:LocationDetails>
            </ns10:Location>
            <ns10:Location>
               <ns10:LocationId>
                  <ns5:RowId>7080018</ns5:RowId>
               </ns10:LocationId>
               <ns10:LocationDetails>
                  <ns10:AuditElement/>
                  <ns10:EffectiveDate/>
                  <ns10:LocationName>REMOVEDEPENDENCY004a</ns10:LocationName>**
                  <ns10:RailIncData/>
                  <ns10:CustCity333Name>OAKLAND1</ns10:CustCity333Name>**
                  <ns10:CustCity333Id>OAKLAND</ns10:CustCity333Id>
                  <ns10:CustCity333StateCode>TN</ns10:CustCity333StateCode>
                  <ns10:ParentCIFDetails/>
               </ns10:LocationDetails>
    </ns10:Location>
    </root>"""
    def parsedXml = new XmlSlurper().parseText(xml)
    def data = parsedXml.'**'.findAll{ it.name() == 'Location'}.inject([]){list, loc -> list << new Expando(
         rowId: loc?.LocationId?.RowId?.text(),
         locationName: loc?.LocationDetails?.LocationName?.text(),
         cityName: loc?.LocationDetails?.CustCity333Name?.text()); list }
    if (0< data.size()) {
      def sb = new StringBuffer(data[0].properties.keySet().join(',')).append('\n')
      data.collect { sb.append(it.properties.values().join(',')).append('\n')}
      new File(fileName).write(sb.toString())
    } else {
      println 'No records found'
    }
    

    您可以在线演示检查数据的快速生成方式

相关问题