Groovy script get certain tag(s) from response xml
Hi,
I am rather new to groovy scripting and have a question. I have a xml request that gives a certain response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
<soap:Body>
<ns2:MT_GetValidationRanges_response xmlns:ns2="http://www.eandis.be/CAPTATIE/MF881/SPI893">
<GetValidationRangesResponse>
<EANs>
<EAN>
<EANNumber>541448860004900201</EANNumber>
<MeteringDevices>
<MeteringDevice>
<Id>7100627867</Id>
<SerialNumber>23598095</SerialNumber>
<DeviceLabel>1</DeviceLabel>
<Indexes>
<Index>
<RegisterIdentifier>TH</RegisterIdentifier>
<RegisterSequence>1</RegisterSequence>
<PrevalidationMin>6674</PrevalidationMin>
<PrevalidationMax>7954</PrevalidationMax>
<PrevalidationDigits>8</PrevalidationDigits>
<PrevalidationDecimals>3</PrevalidationDecimals>
</Index>
</Indexes>
</MeteringDevice>
</MeteringDevices>
</EAN>
</EANs>
</GetValidationRangesResponse>
<Status>
<Code>0</Code>
<Description>OK</Description>
</Status>
</ns2:MT_GetValidationRanges_response>
</soap:Body>
</soap:Envelope>
From this reponse I would like to get the value of <PrevalidationMin> and write this to a csv file.
I already found the following code online
def oGroovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def oResponseHolder = oGroovyUtils.getXmlHolder("Single GetValidationRanges" + "#Response")
oResponseHolder.getNodeValue( "PrevalidationMin")
But still need some code. I cant find the last part. Can someone help me please ?
Thanks
Try the below script.
import com.eviware.soapui.support.XmlHolder def xml = new XmlHolder(context.response) def preValidationMinValue = xml.getNodeValue("//*:PrevalidationMin") new File('c:/temp/myfile.csv').write(preValidationMinValue)
Note that, you may have to change/tune the xpath if you have multiple EAN elements to get the right value.