Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SOAPUI中使用Groovy脚本获取文本并存储到Excel工作表中_Groovy_Soapui - Fatal编程技术网

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

在SOAPUI中使用Groovy脚本获取文本并存储到Excel工作表中,groovy,soapui,Groovy,Soapui,我需要在soapui工具中从xml中获取值,并将这些数据存储到Excel工作表中。我在SoapUI工具中使用了groovy脚本 如果响应有多个输出,这些输出存储在excel工作表中。像LocationName和CustCity333Name一样,它们有两个名称,所以这些输出应该存储到excel工作表中。请帮我解决这个问题 <ns10:Location> <ns10:LocationId> <ns5:RowId>

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

如果响应有多个输出,这些输出存储在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>

7080013
**移除的ependency004**
**奥克兰**
奥克兰
总氮
7080018
**移除的ependency004**
**奥克兰**
奥克兰
总氮
注意:

  • 由于您提到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'
}
//在下面提供/编辑csv文件的文件路径
def fileName='/tmp/locationData.csv'
def xml=“”
7080013
移除的ependency004**
奥克兰**
奥克兰
总氮
7080018
已删除的附件004A**
奥克兰1**
奥克兰
总氮
"""
def parsedXml=new XmlSlurper().parseText(xml)

def data=parsedXml.'**'.findAll{it.name()=='Location'}.inject([]){list,loc->list是从xml中提取数据的问题吗?还是写入excel?是的,Rao,数据需要提取并存储到excel工作表中预期结果如下所示:在excel中删除ePendency004奥克兰删除ePendency004奥克兰删除ePendency004奥克兰您是否有任何在excel中存储数据的脚本/脚本?您只是坚持使用excel还是打开到csv?我不知道我没有任何脚本。只要我使用excel或CSV,如果有更多的
LocationDetails
,列数据的数量就会增加,对吗?您试图通过在其中存储数据来实现什么?非常感谢Rao,代码正在工作感谢您的努力。