28875

Using a variable to extract value of property of an element in groovy using xmlSlurper

Question:

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 <a href="https://stackoverflow.com/questions/11506920/using-a-string-as-code-with-groovy-xml-parser?lq=1" rel="nofollow">Using a String as code with Groovy XML Parser</a>, but it doesn't serve my purpose.

Any other ideas?

Answer1:

You can use

x."$requestName".each

Answer2:

Why XmlSlurper? In SoapUI you can use XmlHolder

import com.eviware.soapui.support.XmlHolder def responseHolder = new XmlHolder(messageExchange.getResponseContentAsXml()) def resultFromResponse = responseHolder["here_is_your_xpath"] assert someExpectedResult == resultFromResponse

And if you need to iterate via multiple xml nodes

resultFromResponse.each { assert result == resultFromResponse }

Recommend

  • Using a variable to extract value of property of an element in groovy using xmlSlurper
  • groovy.json.JsonSlurper parse JSON
  • How to pass a text from one activity to all activities?
  • Transfer Property ids (Array) to other TestCases in SoapUI/Groovy
  • Call .dll functions using Java
  • xml to json conversion in Groovy
  • CodeIgniter - query binding “IN” parameter
  • How to update nested objects in Elasticsearch 2.2 script via Java API
  • How to send JSON array containg jsonobjects to php server [closed]
  • “Injection” concerns for Cypher over REST
  • neo4jClient create node with dynamic label using paramters
  • Rails 4.0.0 jQuery Mobile button icons not showing in production
  • Simple test app deploys to Heroku but won't run
  • ASP.NET, C# How to Pass a StringQuery to a custom SQL Command
  • What's the syntax to inherit documentation from another indexer?
  • Why the SequenceFile is truncated?
  • DIV instruction jumping to random location?
  • Cast between interfaces whose interface signatures are same
  • C++ Partial template specialization - design simplification
  • Xamarin Forms - UWP Fonts
  • Javascript simulate pressing enter in input box
  • HTML download movie download link
  • How to add date and time under each post in guestbook in google app engine
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • JSON with duplicate key names losing information when parsed
  • How do you troubleshoot character encoding problems?
  • Return words with double consecutive letters
  • How to format a variable of double type
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How to Embed XSL into XML