使用xmlSlurper在groovy中使用变量提取元素属性的值 [英] Using a variable to extract value of property of an element in groovy using xmlSlurper

查看:595
本文介绍了使用xmlSlurper在groovy中使用变量提取元素属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SoapUI来测试web服务。以下字符串(采用xml格式)是我的要求:

 < Request> 
< AC>
< AssetID> 1< / AssetID>
< Asset_Name> ABC< / Asset_Name>
< Asset_Number> 1< / Asset_Number>
< / AC>
< AC>
< AssetID> 2< / AssetID>
< Asset_Name> XYZ< / Asset_Name>
< Asset_Number> 2< / Asset_Number>
< / Ac>
< / Request>

我在groovy脚本中使用以下代码为每个AC提取Asset_Number的值xml string存储在变量strRequest中):

  def x = new XmlSlurper()。parseText($ strRequest)

x.AC.each {AC - >
assetNum = AC。Asset_Number
<<<<对assetNum>做些什么>>
}

然而,我希望参数化上面的代码来获取各种类型的Asset_Number的资产(例如AC,外围设备等)。每个资产的请求xml格式与上面相同。如果我在上面的代码中用变量名'requestName'替换'AC':

  // strRequest = xml请求
def requestName //我将从一个测试用例属性中为它提取值
def x = new XmlSlurper()。parseText($ strRequest)

x。(requestName.toString( ))。each {requestName - >
assetNum = requestName。Asset_Number
<<<<对assetNum>做些什么>>
}

显示以下错误:

  org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:Script166.groovy:35:当前作用域已经包含一个名为requestName @行35的变量,第2列。{^ org.codehaus.groovy.syntax.SyntaxException:当前作用域已经包含名称变量requestName 

我尝试了另一篇文章中提到的解决方案使用String作为Groovy XML Parser的代码,但它不起作用。



其他想法?

解决方案

您可以使用

  x。$ requestName.each 


I am using SoapUI to test webservices. The following string (in xml format) is my request:

<Request>
   <AC>
      <AssetID>1</AssetID>
      <Asset_Name>ABC</Asset_Name>
      <Asset_Number>1</Asset_Number>
   </AC>
   <AC>
      <AssetID>2</AssetID>
      <Asset_Name>XYZ</Asset_Name>
      <Asset_Number>2</Asset_Number>
   </Ac>
</Request>

I am using the following code in a groovy script to extract value of Asset_Number for each AC (The above xml string is stored in variable strRequest):

def x = new XmlSlurper().parseText("$strRequest")

x.AC.each { AC ->
assetNum = AC."Asset_Number"
<<do something with the assetNum>>
}

However, I wish to parameterize the above code to pick up Asset_Number for various types of assets (e.g. AC, Peripheral etc). The request xml for each asset is in the same format as above. If I replace 'AC' with variable name 'requestName' in above code:

//strRequest = xml request
def requestName //I will pick up value for this from a test case property
def x = new XmlSlurper().parseText("$strRequest")

x.(requestName.toString()).each { requestName ->
    assetNum = requestName."Asset_Number"
    <<do something with the assetNum>>
}

it shows the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script166.groovy: 35: The current scope already contains a variable of the name requestName @ line 35, column 2. { ^ org.codehaus.groovy.syntax.SyntaxException: The current scope already contains a variable of the name requestName

I have tried a solution mentioned in another post Using a String as code with Groovy XML Parser, but it doesn't serve my purpose.

Any other ideas?

解决方案

You can use

x."$requestName".each

这篇关于使用xmlSlurper在groovy中使用变量提取元素属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆