如何使用groovy脚本循环遍历XML子节点

时间:2017-02-24 03:32:57

标签: xml groovy soapui assertion xmlslurper

我有一个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()
    }

请帮助我迭代断言的节点详细信息(名称和区域)和地址(代码和国家/地区)

1 个答案:

答案 0 :(得分:0)

首先,看起来您的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"
}
相关问题