2017-02-24 55 views
1

節點我有如下的XML響應:如何遍歷XML孩子使用Groovy腳本

<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>[email protected]</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>[email protected]</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> 

我想這個迭代的childNodes細節和地址驗證與請求屬性響應。但我可以斷言電子郵件,但無法詳細(姓名和地區)和地址(代碼和國家)。下面是我使用

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() 
    } 

請幫我在遍歷節點的詳細信息(姓名和區域及地址碼和國家)的斷言

+0

你的意思是,如果你收到的響應,相同的數據在被送往說請求?然後,請添加您的請求以及編輯帖子。 – Rao

+0

Nope請求與響應數據不同。這是不同的,我設置屬性與價值觀分開。我想通過驗證具有已設置屬性的xml數據來斷言它。 – Kumar

回答

0

首先的代碼,似乎你的XML是稍微打破了缺少結束標籤。我冒昧地在下面的例子中解決這個問題。

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

從XmlSlurper返回的xml節點表示爲groovy GPathResult類的後代。這些後代包括NodeChild,NodeChildren,NoChildren和Attribute,根據查詢和xml的外觀,所有這些都可以由xml.Envelope.Body.Response類型的查詢返回。要檢索節點內的實際文本數據,您需要撥打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>[email protected]</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>[email protected]</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    == '[email protected]' 
assert contactNodes.first().details.name.text() == "Kevin" 
assert contactNodes.first().details.area.text() == "Networking" 

assert contactNodes.last().email    == '[email protected]' 
assert contactNodes.last().details.name.text() == "John" 
assert contactNodes.last().details.area.text() == "Development" 

運行,所有的斷言成功。

contactNodes變量是被視爲一個節點列表(即你可以調用方法如.each {}.every {}.any {},...就可以了)一個groovy NodeChildren對象,可以爲所有意圖和目的。響應

編輯評論:僅在具有特殊性能的接觸節點迭代,你可以這樣做:

xml.Body.response.customer.contact.findAll { contactNode -> 
    contactNode.type.text() == 'firstclass' 
}.each { firstClassContactNode -> 
    assert firstClassContactNode.email.text() == "[email protected]" 
} 
+0

感謝您的回答,您可以告訴我,如果我想在類型爲firstclass時迭代xml。我想迭代這些XML數據並聲明它 – Kumar

+0

感謝Matias,我已經可以斷言該類型的電子郵件地址,你可以幫我聲明細節並解決該類型的子節點 – Kumar

+0

我試過這個。 .each {details - > assert details.name.text()==「Kevin」}獲取[email protected](to.String()==「」)消息 – Kumar