简体   繁体   中英

How to loop over XML tags using groovy script

I have an XML response with namespaces as below:

<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. I have a specific value for "assetSerialNumber" that will return in the one of the "nonIRDAssetInformation" the index of which i am not sure.

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.

I am new to groovy scripting, and i had written the below script after doing some research.

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={}; value=[{ 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()
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM