SoapUI repsonse解析

时间:2016-07-12 15:46:05

标签: groovy soapui

我正在使用groovy脚本来解析使用SoapUI的API调用的响应。

我得到的回应按照确切的顺序有以下要素 -

<DeviceOS>
<Bids>
<DeviceOSTargetBid>
<BidAdjustment>2</BidAdjustment>
<DeviceName>Computers</DeviceName>
</DeviceOSTargetBid>

<DeviceOSTargetBid>
<BidAdjustment>32</BidAdjustment>
<DeviceName>Smartphones</DeviceName>
</DeviceOSTargetBid>

<DeviceOSTargetBid>
<BidAdjustment>0</BidAdjustment>
<DeviceName>Tablets</DeviceName>
</DeviceOSTargetBid>

</Bids>
</DeviceOS>

我想访问每个BidAdjustment和DeviceName元素并将它们存储在一个文件中。为此,我使用名为 holder 的XML持有者对象,并使用代码 holder.getNodeValue(“// *:BidAdjustment”)

但是,这只是返回第一个值(即计算机为2)。我怎么得到其他的?所有的名字都是一样的,因此我不能用不同的名字来引用它们。帮助将不胜感激:) :)谢谢:))

1 个答案:

答案 0 :(得分:0)

我猜您使用的是com.eviware.soapui.support.GroovyUtils班级的持有人。

你得到的只是一个节点。 您可能应该使用def nodes = holder.getDomNodes("//*:DeviceOSTargetBid")代替。

然后,您可以通过xpath表达式迭代每个返回的节点。

<强> 已更新

def items = holder.getNodeValues("//*:DeviceOSTargetBid")
for (node in items) {    
   node.BidAdjustment.text(); 
   node.DeviceName.text();       
}

一些有用的文档: http://docs.groovy-lang.org/latest/html/gapi/groovy/xml/dom/DOMCategory.html

或者,如果您希望改变获得回复的方式,可以尝试这种方式:

def rootNode = new XmlSlurper().parseText(
    '''<DeviceOS>
<Bids>
<DeviceOSTargetBid>
<BidAdjustment>2</BidAdjustment>
<DeviceName>Computers</DeviceName>
</DeviceOSTargetBid>

<DeviceOSTargetBid>
<BidAdjustment>32</BidAdjustment>
<DeviceName>Smartphones</DeviceName>
</DeviceOSTargetBid>

<DeviceOSTargetBid>
<BidAdjustment>0</BidAdjustment>
<DeviceName>Tablets</DeviceName>
</DeviceOSTargetBid>

</Bids>
</DeviceOS>''' )

def f = new File('C:/yourFileName.txt')
rootNode.Bids.DeviceOSTargetBid.each(){ s ->
    f << "${s.DeviceName.text()} ${s.BidAdjustment.text()}" + '\n'
}
相关问题