简体   繁体   English

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

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

I need to get value from xml in soapui tool and store those data into Excel sheet. 我需要从soapui工具中的xml中获取价值,并将这些数据存储到Excel工作表中。 I used groovy script in SoapUI tool. 我在SoapUI工具中使用了Groovy脚本。

If Response have multiple output and these output store in excel sheet. 如果Response具有多个输出,并且这些输出存储在excel表中。 Like LocationName and CustCity333Name have twice, so these output should store into excel sheet. 像LocationName和CustCity333Name有两次一样,因此这些输出应存储到excel工作表中。 Please help me to resolve this issue 请帮我解决这个问题

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

NOTE: 注意:

  • Since you mentioned excel or csv is fine, below script uses csv format. 由于您提到excel或csv都很好,因此以下脚本使用csv格式。
  • The data you provided is not well-formed. 您提供的数据格式不正确。 Modified a bit to make it well-formed. 进行了一些修改以使其格式正确。
  • Also using rowId for identification in the row. 还使用rowId在行中进行标识。 You may remove if you do not wish. 如果不希望,可以将其删除。

Groovy Script 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'
}

You can check how data is generated quickly online Demo 您可以检查如何快速在线生成数据演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM