Forum Discussion
Hi,
Ok, so if I may suggest the use of XMLSlurper, which is built into Groovy so requires no imports and is quote neat. Then if we put your XML snippet (similar typed in by me) into a file /work/SoapUI Forum/soapy.xml:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">
<soap:Header/>
<soap:Body>
<tem:Add xmlns:tem="http://tempuri.org">
<tem:intA>5</tem:intA>
<tem:intB>7</tem:intB>
</tem:Add>
</soap:Body>
</soap:Envelope>
Then, the following Groovy script allows you to parse the XML from file, update the values of intA and intB and save the resulting XML back to the same file:
import groovy.xml.XmlUtil
def xmlFromFile = new File('/work/SoapUI Forum/soapy.xml')
def envelopeNode = new XmlSlurper().parseText(xmlFromFile.getText())
envelopeNode.Body.Add.intA=5
envelopeNode.Body.Add.intB=7
//Just to print it out
XmlUtil xmlUtil = new XmlUtil()
log.info xmlUtil.serialize(envelopeNode)
//To update the file with updated XML
xmlUtil.serialize(envelopeNode, new FileWriter(xmlFromFile))
Is this what you needed?
Cheers,
Rup
Thanks Rup!!
This is what I was looking for. But how to parameterize both xpath and and node value. Lets say, I am taking this xpath (envelopeNode.Body.Add.intA) as a input parameter from excel sheet and then modifying the value to '20'.
def sIntA=envelopeNode.Body.Add.intA // Lets say, this value is taken from excel sheet (I have a code to get the value from excel sheet)
My aim is to get the Node's xpath from excel sheet, get the Node's value from excel sheet, update the xml with this node and save updated xml in a different folder.
Please suggest.