如何在soapUI中使用XmlSlurper

时间:2011-04-24 19:24:17

标签: groovy soapui

我有以下groovy脚本,我在groovyconsole中运行它运行得很好。我在xml响应中找到特定节点的子节点数,并为每个子节点打印出所需的值。

def path = new XmlSlurper().parse(new File('C://SoapUI//ResponseXML/Response.xml'))
NumberOfPositions =  path.Body.GetPositionsAggregateResponse.GetPositionsAggregateResult.AccountPositions.Securities.Positions.children().size()
for(def i=0; i<NumberOfPositions; i++){
println i

println  path.Body.GetPositionsAggregateResponse.GetPositionsAggregateResult.AccountPositions.Securities.Positions.PositionSummary[i].Legs[0].PositionAggregate[0].PositionID[0].text()
println  path.Body.GetPositionsAggregateResponse.GetPositionsAggregateResult.AccountPositions.Securities.Positions.PositionSummary[i].Legs[0].PositionAggregate[0].AccountID[0].text()

}

我想在soapUI中执行相同的任务,但是无法使用如下所述的groovyutils工作:http://www.soapui.org/Scripting-Properties/tips-a-tricks.html

1)如何将xml响应从我的请求解析为xmlSlurper? def path = new XmlSlurper()。parse(?)

2)我是否可以在soapUI中使用上述相同的代码?

感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:3)

(1)
要解析响应消息,您可以尝试以下操作:

def response = context.expand( '${TestRequest#Response}' )
def xml = new XmlSlurper().parseText(response)

TestRequest表示发送SOAP请求消息的测试步骤的名称。

(2)
是的,soapUI应该能够处理任何Groovy代码。

答案 1 :(得分:0)

您可以直接在SoapUI中使用普通的groovy脚本。检查此link,它可能会对您有所帮助。但是,请记住,而不是“println”,而不是在SoapUI脚本编写时使用“log.info”。