2017-02-24 48 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