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

问题描述 投票:0回答:2

我有一个 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>[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>

我正在尝试迭代子节点详细信息和地址,以验证请求属性的响应。但我可以断言电子邮件,但无法详细说明(名称和区域)和地址(代码和国家/地区)。以下是我正在使用的代码

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 groovy soapui assertion xmlslurper
2个回答
2
投票

首先,您的 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>[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
变量是一个groovy NodeChildren对象,并且可以出于所有意图和目的被视为节点列表(即,您可以调用诸如
.each {}
.every {}
.any {}
之类的方法
,...就可以了)。

编辑以响应评论:要仅迭代具有特定属性的接触节点,您可以执行以下操作:

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

0
投票

首先,通过filter type = "secondclass"获取'contact'节点。 其次,根据步骤1的结果,对每一项进行测试。

你的groovy脚本将像这样改变:

    import groovy.xml.*
    
    def envelope = new XmlSlurper().parseText(messageExchange.responseContentAsXml);
    def type = 'secondclass';
    def emailAddress= ${properties#emailAddress};
    
    envelope.Body.response.customer.contact
         .findAll {contactNode -> contactNode.type.text() == type}
         .each{ currentNode -> assert emailAddress== currentNode.emailAddress.text()};
© www.soinside.com 2019 - 2024. All rights reserved.