0

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>
user1514499
  • 762
  • 7
  • 26
  • 63

2 Answers2

3

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())
maba
  • 47,113
  • 10
  • 108
  • 118
  • I added the part for SoapUI. The XmlSlurper is really nice and uses [GPath](http://groovy.codehaus.org/GPath) expressions to navigate in the XML tree. – maba Aug 24 '12 at 09:40
  • Hi maba,Can you please answer this question if possible http://stackoverflow.com/questions/12106955/groovy-script-to-get-the-request-xml. Sorry I am actually need to get the values from the request xml – user1514499 Aug 24 '12 at 09:48
2

Check out XmlParser or XmlSlurper examples in Groovy documentation page

Anton Arhipov
  • 6,479
  • 1
  • 35
  • 43