我只使用JSON响应和验证我使用下面的脚本。现在我需要对XML响应进行类似的验证。如何为XML实现这一目标?
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText response
assert json.name == "ABCD"
assert json.status == "Success"
答案 0 :(得分:2)
您可以简单地使用与JsonSlurper
非常相似的XmlSlurper
类。假设这是你的XML,你可以这样做:
def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>
'''
def root = new XmlSlurper().parseText(xml)
assert root.food[0].name.text() == 'Belgian Waffles'
请记住new XmlSlurper().parseText(xml)
返回引用第一个(根)XML节点元素的节点。然后,您可以执行与JsonSlureper
类相同的操作。