xml parsing - Using a variable to extract value of property of an element in groovy using xmlSlurper -
i using soapui test webservices. following string (in xml format) 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 using following code in groovy script extract value of asset_number each ac (the above xml string stored in variable strrequest):
def x = new xmlslurper().parsetext("$strrequest") x.ac.each { ac -> assetnum = ac."asset_number" <<do assetnum>> }
however, wish parameterize above code pick asset_number various types of assets (e.g. ac, peripheral etc). request xml each asset in same format above. if replace 'ac' variable name 'requestname' in above code:
//strrequest = xml request def requestname //i pick value test case property def x = new xmlslurper().parsetext("$strrequest") x.(requestname.tostring()).each { requestname -> assetnum = requestname."asset_number" <<do assetnum>> }
it shows following error:
org.codehaus.groovy.control.multiplecompilationerrorsexception: startup failed: script166.groovy: 35: current scope contains variable of name requestname @ line 35, column 2. { ^ org.codehaus.groovy.syntax.syntaxexception: current scope contains variable of name requestname
i have tried solution mentioned in post using string code groovy xml parser, doesn't serve purpose.
any other ideas?
you can use
x."$requestname".each
Comments
Post a Comment