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

Get Text and store into Excel sheet using Groovy Script in SOAPUI

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

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

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

注意:

  • 既然您提到 excel 或 csv 都可以,下面的脚本使用 csv 格式。
  • 您提供的数据格式不正确。稍微修改一下,使其格式正确。
  • 也使用rowId在行中进行标识。如果您不希望,可以删除。

Groovy 脚本

//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'
}

你可以在线查看数据是如何快速生成的Demo