Reputation: 771
I need to get the value 3 from the below xml file using groovy script. I am testing from SOAPUI
<ParamId>3</ParamId>
Can anyone please share me the syntax to get the value? I tried the following code. But I think I need to iterate to the third Param..
def msgTxt = response.getDomNode("//ns2:ParamId").getLastChild().getNodeValue()
<RequestParams>
<Param>
<ParamId>1</ParamId>
<ParamName>Name1</ParamName>
<ParamType>String</ParamType>
<ParamValue>value1</ParamValue>
</Param>
<Param>
<ParamId>2</ParamId>
<ParamName>Name2</ParamName>
<ParamType>String</ParamType>
<ParamValue>value2</ParamValue>
</Param>
<Param>
<ParamId>3</ParamId>
<ParamName>Name3</ParamName>
<ParamType>String</ParamType>
<ParamValue>2</ParamValue>
</Param>
</RequestParams>
Upvotes: 0
Views: 2114
Reputation: 48105
If you are using groovy scripts then this will work.
parse.groovy
import org.apache.maven.artifact.ant.shaded.xml.XmlStreamReader
/**
* @author maba, 2012-08-24
*/
def root = new XmlSlurper().parse(new XmlStreamReader(new File('path/to/data', 'data.xml')))
def msgText = root.Param[2].ParamId.text()
And I think you can do this instead of reading from file:
def root = new XmlSlurper().parseText(response.xmlText())
But I am not sure what the type of response
is. Here I assumed an XmlTokenSource
from XmlBeans
.
This should work for SoapUI XmlHolder
:
def root = new XmlSlurper().parseText(response.getXml())
Upvotes: 3
Reputation: 6591
Check out XmlParser or XmlSlurper examples in Groovy documentation page
Upvotes: 2