XML標籤與命名空間的XML響應如下:我如何循環使用Groovy腳本
<tns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
<tns:Body>
<svc:response xmlns:svc="http://...serviceNameSpace"
xmlns:ent="http://....entitiesNameSpace">
<svc:customerList>
<svc:customer>
<svc:nonIRDAssetInformationList>
<svc:nonIRDAssetInformation>
<ent:assetId>AssetId1</ent:assetId>
<ent:assetSerialNumber>SerialNum1</ent:assetSerialNumber>
<ent:assetType>AssetType1</ent:assetType>
</svc:nonIRDAssetInformation>
<svc:nonIRDAssetInformation>
<ent:assetId>AssetId2</ent:assetId>
<ent:assetSerialNumber>SerialNum2</ent:assetSerialNumber>
<ent:assetType>AssetType2</ent:assetType>
</svc:nonIRDAssetInformation>
</svc:nonIRDAssetInformationList>
</svc:customer>
</svc:customerList>
</svc:response >
</tns:Body>
</tns:Envelope>
在SOAPUI的響應窗口此XML響應。 我有一個「assetSerialNumber」的具體值,它將返回其中的「nonIRDAssetInformation」其中我不確定的索引之一。
現在我的要求是循環所有的「nonIRDAssetInformation」來檢查哪個迭代具有特定的值,我需要保存「assetId」標記的值。
我對groovy腳本很陌生,在做了一些研究之後我寫了下面的腳本。
import com.eviware.soapui.support.XmlHolder
//def holder = new XmlHolder(messageExchange.responseContentAsXml)
def Envelope = new XmlParser().parseText(messageExchange.responseContentAsXml)
def tns_ns = new groovy.xml.Namespace("http://..../envelope/", "tns")
def ent_ns = new groovy.xml.Namespace("http://..../entities/", "ent")
def svc_ns = new groovy.xml.Namespace("http://..../services", "svc")
def root = new XmlSlurper().parse(Envelope)
def serialNum= specific value is saved here
def nonIRDAssetInformationList = root.'**'.findAll{
it.name()=='nonIRDAssetInformation'
}
nonIRDAssetInformation.each{
it.assetSerialNumber.text().contains(serialNum)
messageExchange.modelItem.testStep.testCase.testSuite.setPropertyValue("ClientAssetId",it.assetId.text() as String);
}
,當我跑的劇本,我發現了以下錯誤
法無簽名:groovy.util.XmlSlurper.parse()是參數類型適用 :(groovy.util .Node)值: [{http://schemas.xmlsoap.org/soap/envelope/} Envelope [attributes = {}; 值= [{} http://schemas.xmlsoap.org/soap/envelope/頭[屬性= {}; .....
是否有任何一個誰可以幫助我得到一個解決方案。
你爲什麼試圖解析XmlParser的通過的XmlSlurper解析輸出? –
你不能使用包含斷言嗎?這將檢查您的整個XML的序列號。除非您希望序列號出現在XML中的其他位置,否則您應該沒問題。 –