如何在SoapUI中捕获Soap响应中的标记值,并将其写入Excel

时间:2015-08-10 20:39:23

标签: excel soap groovy soapui

我已经使用POI从Excel中读取,现在我试图捕获响应并将其写回Excel,这里是代码

 <AtomicWeight>1.00797</AtomicWeight>

我想从响应中将1.00797写入excel中。但是请求中不存在此标记。有什么方法可以实现吗?

//响应消息

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetAtomicWeightResponse xmlns="http://www.webserviceX.NET">
         <GetAtomicWeightResult><![CDATA[<NewDataSet>
  <Table>
 // want to capture this value(1.00797) and write it in excel
   <AtomicWeight>1.00797</AtomicWeight>
  </Table>
</NewDataSet>]]></GetAtomicWeightResult>
      </GetAtomicWeightResponse>
   </soap:Body>
</soap:Envelope>

在这里输入代码

在这里输入代码

1 个答案:

答案 0 :(得分:0)

访问AtomicWeight元素很棘手,因为它包含在CDATA中。 XmlSlurper无法解析它。但你可以通过...来实现它。

  1. 解析整个SOAP信封
  2. 抓取GetAtomicWeightResult元素并转换为 串
  3. 解析GetAtomicWeightResult String-ified元素。
  4. 抓住AtomicWeight元素。
  5. 怎么做

    import groovy.util.XmlSlurper
    
    def text = '''<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <soap:Body>
          <GetAtomicWeightResponse xmlns="http://www.webserviceX.NET">
             <GetAtomicWeightResult><![CDATA[<NewDataSet>
      <Table>
     // want to capture this value(1.00797) and write it in excel
       <AtomicWeight>1.00797</AtomicWeight>
      </Table>
    </NewDataSet>]]></GetAtomicWeightResult>
          </GetAtomicWeightResponse>
       </soap:Body>
    </soap:Envelope>'''
    
    def xml = new XmlSlurper().parseText(text)
    def cdata = new XmlSlurper().parseText(xml.Body.GetAtomicWeightResponse.GetAtomicWeightResult.toString())
    
    assert cdata.Table.AtomicWeight == 1.00797