[英]How to loop over XML tags using groovy script
I have an XML response with namespaces as below: 我有一个带有名称空间的XML响应,如下所示:
<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>
This response XML in the response window of SoapUi. 此响应XML在SoapUi的响应窗口中。 I have a specific value for "assetSerialNumber" that will return in the one of the "nonIRDAssetInformation" the index of which i am not sure. 我有一个“ assetSerialNumber”的特定值,该值将在我不确定其索引的“ nonIRDAssetInformation”之一中返回。
Now my requirement is to loop over all the "nonIRDAssetInformation" to check which iteration has the specific value and i need to save the value of "assetId" tag. 现在,我的要求是遍历所有“ nonIRDAssetInformation”,以检查哪个迭代具有特定值,并且我需要保存“ assetId”标签的值。
I am new to groovy scripting, and i had written the below script after doing some research. 我是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);
}
When i ran the script i'm getting the below error 当我运行脚本时,出现以下错误
No signature of method: groovy.util.XmlSlurper.parse() is applicable for argument types: (groovy.util.Node) values: [{ http://schemas.xmlsoap.org/soap/envelope/ }Envelope[attributes={}; 没有方法签名:groovy.util.XmlSlurper.parse()适用于参数类型:(groovy.util.Node)值:[{ http://schemas.xmlsoap.org/soap/envelope/ } Envelope [attributes = {}; value=[{ http://schemas.xmlsoap.org/soap/envelope/ }Header[attributes={};..... 值= [{ http://schemas.xmlsoap.org/soap/envelope/ } Header [attributes = {}; .....
Is there any one who can help me in getting a solution for this. 有谁能帮助我找到解决方案。
You appear to be trying to parse already parsed results (not sure why) 您似乎正在尝试解析已解析的结果(不确定原因)
Something like this should work for you: 这样的事情应该为您工作:
import groovy.xml.*
def envelope = new XmlSlurper().parseText(messageExchange.responseContentAsXml)
def serialNum = 'Num'
envelope.'**'
.findAll { it.name() == 'nonIRDAssetInformation' }
.findAll { it.assetSerialNumber.text().contains(serialNum) }
.each {
println it.assetId.text()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.