簡體   English   中英

如何使用groovy腳本循環遍歷XML子節點

[英]How to loop over XML child nodes using groovy script

我有一個XML響應如下:

<ns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
 <ns:Body>
    <ns:response xmlns:svc="http://...serviceNameSpace" 
                xmlns:ent="http://....entitiesNameSpace">
        <ns:customer>
            <ns:contact>
                <ns:type>firstclass</ns:type>
                <ns:email>kevin@....com</ns:email>
                <ns:details>
                    <ns:name>Kevin</ns:name>
                    <ns:area>Networking</ns:area>
                </ns:details>
                <ns:address>
                    <ns:code>39343</ns:code>
                    <ns:country>US</ns:country>
                </ns:address>
             </ns:contact>
            <ns:contact>
                <ns:type>secondclass</ns:type>
                <ns:email>john@...com</ns:email>
                <ns:details>
                    <ns:name>John</ns:name>
                    <ns:area>Development</ns:area>
                <ns:address>
                    <ns:code>23445</ns:code>
                    <ns:country>US</ns:country>
             </ns:contact>                 
        </ns:customer>
    </ns:response >
</ns:Body>

我正在嘗試迭代子節點詳細信息和地址以使用請求屬性驗證響應。 但我可以斷言電子郵件,但無法詳細說明(姓名和地區)和地址(代碼和國家/地區)。 以下是我正在使用的代碼

import groovy.xml.*

def envelope = new XmlSlurper().parseText(messageExchange.responseContentAsXml)
def type = 'secondclass'
def emailAddress= ${properties#emailAddress}

envelope.'**'
.findAll { it.name() == 'contact' }
.findAll { it.type.text().contains(type) }
.each {
        assert emailAddress== it.emailAddress.text()
    }

請幫助我迭代斷言的節點詳細信息(名稱和區域)和地址(代碼和國家/地區)

首先,似乎你的xml因缺少結束標記而略微破碎。 我冒昧地在下面的例子中解決了這個問題。

從概念上講,當您使用xml.Envelope.Body.response表達式瀏覽xml時,您將瀏覽xml節點。 請注意xml節點(即元素)與節點內的實際數據或文本之間的區別。

從XmlSlurper返回的xml節點表示為groovy GPathResult類的后代。 這些后代包括NodeChild,NodeChildren,NoChildren和Attribute,所有這些都可以通過xml.Envelope.Body.Response類型的查詢返回,具體取決於查詢和xml的外觀。 要檢索節點內的實際文本數據,需要調用node.text()

修復了xml以及上面的內容,代碼如下:

def str = '''\
<ns:Envelope xmlns:ns="http://schemas.xmlsoap.org/soap/envelope/">
<ns:Body>
    <ns:response xmlns:svc="http://...serviceNameSpace" xmlns:ent="http://....entitiesNameSpace">
        <ns:customer>
            <ns:contact>
                <ns:type>firstclass</ns:type>
                <ns:email>kevin@....com</ns:email>
                <ns:details>
                    <ns:name>Kevin</ns:name>
                    <ns:area>Networking</ns:area>
                </ns:details>
                <ns:address>
                    <ns:code>39343</ns:code>
                    <ns:country>US</ns:country>
                </ns:address>
             </ns:contact>
            <ns:contact>
                <ns:type>secondclass</ns:type>
                <ns:email>john@...com</ns:email>
                <ns:details>
                    <ns:name>John</ns:name>
                    <ns:area>Development</ns:area>
                </ns:details>
                <ns:address>
                    <ns:code>23445</ns:code>
                    <ns:country>US</ns:country>
                </ns:address>
             </ns:contact>                 
        </ns:customer>
    </ns:response >
</ns:Body>
</ns:Envelope>
'''

def xml = new XmlSlurper(false, true).parseText(str)

def contactNodes = xml.Body.response.customer.contact

assert contactNodes.first().email               == 'kevin@....com'
assert contactNodes.first().details.name.text() == "Kevin"
assert contactNodes.first().details.area.text() == "Networking"

assert contactNodes.last().email               == 'john@...com'
assert contactNodes.last().details.name.text() == "John"
assert contactNodes.last().details.area.text() == "Development"

運行並且所有斷言都成功。

contactNodes變量是一個groovy NodeChildren對象,可以將所有意圖和目的都視為節點列表(即你可以調用.each {} .every {} .any {} ,...等方法) 。

編輯以響應評論 :要僅對具有特定屬性的聯系節點進行迭代,您可以執行以下操作:

xml.Body.response.customer.contact.findAll { contactNode ->
    contactNode.type.text() == 'firstclass' 
}.each { firstClassContactNode -> 
    assert firstClassContactNode.email.text() == "kevin@....com"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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