解析Soapui xml响应

时间:2017-01-11 11:41:27

标签: xml soapui

XML示例:

<soapenv:Envelope>
    .....
</soapenv:Envelope>

我正在使用以下脚本遍历

def authorResult=soapenv:Envelope;

抛出的错误是“发现”:'

1 个答案:

答案 0 :(得分:0)

您可以通过2解决方案访问XMl响应。

解决方案1 ​​

def xml = new groovy.util.XmlParser().parseText('''\
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:creditCard">
    <SOAP-ENV:Body>
        <ns1:creditCardResponse xmlns:ns1="urn:creditCard">
            <return xsi:type="tns:RPResponse">
                <Status xsi:type="xsd:int">111</Status>
                <Value xsi:type="xsd:string">BBBB</Value>
            </return>
        </ns1:creditCardResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
''')

// XXX namespaces quoted
assert xml.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Status.text()=='111'
log.info xml.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Status.text()
assert xml.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Value.text()=='BBBB'
log.info xml.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Value.text()

解决方案2

def xml = new groovy.util.XmlParser().parseText('''\
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:creditCard">
    <SOAP-ENV:Body>
        <ns1:creditCardResponse xmlns:ns1="urn:creditCard">
            <return xsi:type="tns:RPResponse">
                <Status xsi:type="xsd:int">111</Status>
                <Value xsi:type="xsd:string">BBBB</Value>
            </return>
        </ns1:creditCardResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
''')
    // XXX access by namespace
    def nsSoapEnv = new groovy.xml.Namespace('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV')
    def nsNs1 = new groovy.xml.Namespace('urn:creditCard', 'ns1')
    assert xml[nsSoapEnv.Body][nsNs1.creditCardResponse].return.Status.text()=='111'
    assert xml[nsSoapEnv.Body][nsNs1.creditCardResponse].return.Value.text()=='BBBB'

请您查看此帖子。Read xml values in namespaces/SOAP response using groovy XMLParser