从Soap UI中的响应将节点数据传输到XML文件

时间:2014-06-25 21:04:32

标签: xml groovy soapui

如果我在Soap UI中有以下响应:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ConvertTempResponse xmlns="http://www.webserviceX.NET/">
      <ConvertTempResult>40</ConvertTempResult>
      </ConvertTempResponse>
   </soap:Body>
</soap:Envelope>

我可以使用以下方法将整个回复复制到文本文件中:

 //create folder and file. 
 createFolder = new File("C:/SOAPUI")  
 createFolder.mkdir()
 file = new File("C:/SOAPUI/test.txt")
 file.createNewFile()

 a = testRunner.testCase.getTestStepByName("Property Transfer")
 responsedata = a.getProperty('transfer')

 file.write(responsedata)

如何获取复制的特定节点的数据。例如,如果需要&#39; 40&#39;仅复制。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

您可以使用XmlHolder然后应用XPath来获取节点值,请参阅以下代码:

//create folder and file
createFolder = new File("C:/SOAPUI")  
createFolder.mkdir()
file = new File("C:/SOAPUI/test.txt")
file.createNewFile()

a = testRunner.testCase.getTestStepByName("Property Transfer")
responsedata = a.getProperty('transfer')

// create an XmlHolder
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
// get the response as string
def responseAsString = responsedata.getProperty('response').getValue()    
def xml = groovyUtils.getXmlHolder(responseAsString)
// get the node value
def nodeValue = xml.getNodeValue("//*:ConvertTempResult")
log.info nodeValue;

file.write(nodeValue)

希望这有帮助,