Parsing Soap response - XML/XPATH (In soapUI) (namespace in SoapUI response)

末鹿安然 提交于 2020-01-05 11:54:04

问题


If I've a Soap response like below

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <ns0:SomeResponse xmlns:ns0="urn:ABC:EFG:HIJ:Some_WS">
         <ns0:ID>6384</ns0: ID>
         <ns0:Some_ID>10530</ns0: Some_ID >
         <ns0:Status>SomeStatus</ns0:Status>
         <ns0:Number>INT1037;INT1027;</ns0: Number>
      </ns0:SomeResponse >
   </soapenv:Body>
</soapenv:Envelope>

How can I retrieve Some_ID value. I am using the below code in SoapUI to retrieve the value of Some_ID.

...
def response = tstep.getPropertyValue("response");
def gutils = new com.eviware.soapui.support.GroovyUtils( context );
def holder = gutils.getXmlHolder("$response");
// define namespace
holder.namespaces["ns0"] = "http://www.w3.org/2001/XMLSchema-instance"
def val1 = holder.getNodeValue("//ns0:SomeResponse/ns0:Some_ID");
log.info(val1)

But log.info is giving me null value.


回答1:


To retrieve just that one specific value, you can use a simple:

def val1 = context.expand('${TestStepName#Response#//*:Some_ID}')

For more complex parsing, you would have use to either XmlHolder or XmlParser or XmlSlurper. You can get an idea about these from the official documentation.

In your script, try using tstep.getPropertyValue("Response"), with upper-case R.




回答2:


I got it working. I had to change namespaces declaration to below and thats it.

holder.namespaces["ns0"] = "urn:ABC:EFG:HIJ:Some_WS"


来源:https://stackoverflow.com/questions/30223774/parsing-soap-response-xml-xpath-in-soapui-namespace-in-soapui-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!