簡體   English   中英

如何使用Groovy腳本循環遍歷XML標簽

[英]How to loop over XML tags using groovy script

我有一個帶有名稱空間的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>

此響應XML在SoapUi的響應窗口中。 我有一個“ 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/ } Header [attributes = {}; .....

有誰能幫助我找到解決方案。

您似乎正在嘗試解析已解析的結果(不確定原因)

這樣的事情應該為您工作:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM