如何访问soap UI响应XML的属性值 [英] How to access the attribute value of soap UI response XML
问题描述
我将响应XML作为
< carrierUDOResponse ReferenceId =Result =SUCCESSxmlns:xsi =xsi:noNamespaceSchemaLocation =CarrierUDOReponse.xsd
<错误>
<错误代码=0description =**我想要ACCESS THIS **property =/>
< / errors>
<警告>
<警报代码=0description =property =/ >
< /警告>
< / errorsAndWarnings>
< / carrierUDOResponse>
< / code> b
在Groovy脚本中,我希望捕获属性的值并将其传递到下一步。我应该如何捕获?
groovy脚本低于
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(Create_shipment#Response)
//响应消息中的循环项目节点
for(item in holder.getNod eValues(//error.code))
log.infoerrors:[$ item]
def no = holder [count(// error)]
解决方案以下是使用 xpath
。
看起来像给定的xml是部分的,而且格式不正确。
为了向您展示如何提取数据,修改了一些xml代码片段。但基本上想法是一样的。
- 创建XmlHolder
- 应用xpath获取值
def xml ='''< carrierUDOResponse ReferenceId = Result =SUCCESS>
< errorsAndWarnings>
<错误>
< error code =1description =code1的描述property =/>
< /错误>
<警告>
< warning code =0description =property =/>
< /警告>
< / errorsAndWarnings>
< / carrierUDOResponse>'''
def holder = new com.eviware.soapui.support.XmlHolder(xml)
//使用xpath检索描述。
def descriptions = holder.getNodeValues(// *:errorsAndWarnings / *:errors / *:error / @ description)
//记录描述
descriptions.each {
log.info错误:$ it
变量说明
包含错误描述列表。
我在下面添加了基于您的声明的信息,您希望使用这些描述下面的步骤。但是,这些信息并不完全适用于您的用例。希望以下内容也会有所帮助。
如果您希望这些数据可用于下一步,以下是可能性。但是这可能会改变数据在下一步中真正需要的方式。 另请注意,此处有可用的错误说明列表。 另一个常规脚本:
可以使用 context
变量将数据/对象从一个groovy脚本步传递到其他groovy步。
例如,您可以在上述groovy脚本中将描述添加到上下文中,以便在以下步骤中可以检索相同的描述。 strong>在另一个测试请求步骤(soap / rest)中使用它:在这里您将能够使用字符串数据,但不能以简单的方式使用真正的列表。
I am very new to Groovy & Soap UI
I have response XML as
<carrierUDOResponse ReferenceId="" Result="SUCCESS" xmlns:xsi="" xsi:noNamespaceSchemaLocation="CarrierUDOReponse.xsd
<errors>
<error code="0" description="**i WANT TO ACCESS THIS**" property=""/>
</errors>
<warnings>
<warning code="0" description="" property=""/>
</warnings>
</errorsAndWarnings>
</carrierUDOResponse>
In the Groovy script I want to capture the value of attribute and pass it to next step. How should I capture?
The groovy script is below
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Create_shipment#Response" )
// loop item nodes in response message
for( item in holder.getNodeValues( "//error.code" ))
log.info "errors : [$item]"
def no = holder["count(//error)"]
Here is the groovy script that extracts the required description attributes using xpath
.
Looks like the given xml is partial, and not well formed.
In order to show you the how to extract the data, modified a bit of your xml snippet. But basically idea is same.
- Create the XmlHolder
- Apply the xpath to get the value
def xml='''<carrierUDOResponse ReferenceId="" Result="SUCCESS">
<errorsAndWarnings>
<errors>
<error code="0" description="Description for code 0" property=""/>
<error code="1" description="Description for code1" property=""/>
</errors>
<warnings>
<warning code="0" description="" property=""/>
</warnings>
</errorsAndWarnings>
</carrierUDOResponse>'''
def holder = new com.eviware.soapui.support.XmlHolder(xml)
//use the xpath to retrieve the desctiption.
def descriptions = holder.getNodeValues("//*:errorsAndWarnings/*:errors/*:error/@description")
//logging the descriptions
descriptions.each{
log.info "Error: $it"
}
Here the variable descriptions
contains the list of error descriptions.
I am adding below information based on your statement that you wanted to use these descriptions in the following step. But, the information is not completely available how your use case is. Hoping that below would be helpful too.
If you want this data to be available to the next step, the following are possibilities. But it may vary how the data is really needed in the next step. Also note that here there are list of error descriptions available.
Use it in another groovy script: It is possible to pass data / object from one groovy script step to other groovy step using
context
variable. For example you can add the descriptions to context in the above groovy script, so that the same can be retrieved in the following step.Use it in another test request step (soap / rest): Here you will be able use a string data, but not really list can be used in a simple way.
这篇关于如何访问soap UI响应XML的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!