我有以下 xml 代码:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<cms:RelatedConfigurationItemList xmlns:cms="some namespace">
<ConfigurationItem>
<name>data</name>
<id>data</id>
<type>data</type>
<relationship>IS CHILD OF</relationship>
<ConfigurationItemList>
<ConfigurationItem>
<name>data</name>
<id>data</id>
<type>data</type>
<relationship>IS CHILD OF</relationship>
<ConfigurationItemList/>
</ConfigurationItem>
<ConfigurationItem>
<name>data</name>
<id>data</id>
<type>data</type>
<relationship>IS CHILD OF</relationship>
<ConfigurationItemList/>
</ConfigurationItem>
</ConfigurationItemList>
</ConfigurationItem>
<ConfigurationItem>
<name>other data</name>
<id>other data</id>
<type>other data</type>
<relationship>IS CHILD OF</relationship>
<ConfigurationItemList>
<ConfigurationItem>
<name>other data</name>
<id>other data</id>
<type>other data</type>
<relationship>IS CHILD OF</relationship>
<ConfigurationItemList/>
</ConfigurationItem>
<ConfigurationItem>
<name>other data</name>
<id>other data</id>
<type>other data</type>
<relationship>IS CHILD OF</relationship>
<ConfigurationItemList/>
</ConfigurationItem>
</ConfigurationItemList>
</ConfigurationItem>
</cms:RelatedConfigurationItemList>
</soapenv:Body>
</soapenv:Envelope>
我想使用以下伪代码在 Groovy 中进行验证:
def request = testRunner.testCase.getTestStepByName( "relationship_request" )
def resp = new File('H://test_xml.xml')
def cms_ns = new groovy.xml.Namespace("namespace for cms",'cms')
def soap_ns = new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/",'soapenv')
def root = new XmlSlurper().parse(resp)
def config_item = root[soap_ns.Envelope][soap_ns.Body][cms_ns.RelatedConfigurationItemList][ConfigurationItem]
config_item.each{
it.name.each{
it == corresponding value in db?
else
die
}
}
但是我似乎无法获得正确的语法和逻辑来尝试针对数据库响应验证定义的值(例如名称)。如果 config_item 声明是正确的,那么可能我对 Groovy 闭包的理解很差。此外,我不确定 XML slurper 或解析器是否更合适,也无法确定到底有什么区别。希望这是对问题的充分描述。