cancel
Showing results for 
Search instead for 
Did you mean: 

How to update tag value in external xml using groovy script?

Occasional Contributor

How to update tag value in external xml using groovy script?

I am trying to update values of node <tem:intA> and <tem:intB> in the following xml using below groovy script code.

 

UpdateXmlTagValue.jpg

 

I used below groovy script code to update the tag values. But I am getting an error "Java.lang.NullPointerException: Cannot invoke method getNodeValue() on null object error at line 36".

 

UpdateXmlTagValue2.jpg

 

Am I using the wrong xpath? I am not sure whats going wrong. Can someone please help me?

 

9 REPLIES 9
Valued Contributor

Re: How to update tag value in external xml using groovy script?

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

Author of SoapUI Cookbook
Occasional Contributor

Re: How to update tag value in external xml using groovy script?

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.

Valued Contributor

Re: How to update tag value in external xml using groovy script?

Hi,

 

Thats ok.

 

I think I now get what you are after i.e. a way to pass the actual (XPath) query in as a String paramter, but before having a go at that, can I ask if you actually just want a way to parameterise and populate SOAP requests from your spreadsheet of values e.g. data-driven testing? If this is the case I can potentially explain an easier way to do this in SoapUI using your spreadsheet.

 

Cheers,
Rup

Author of SoapUI Cookbook
Occasional Contributor

Re: How to update tag value in external xml using groovy script?

 

Yes Rup. I am actually looking for data driven testing. I am trying to use external xml stored on local disc. I will update the node values using xpath. After updating the xml, I will save it in different folder and at the end I will upload both request xml and response xml to the test case in ALM. It helps me keeping track of request and response used for specific test case.

Please let me know if you have any easier way to do this.

Really appreciate your help. Thanks!

 

Cheers,

Vikram

 

 

Valued Contributor

Re: How to update tag value in external xml using groovy script?

Hi Vikram,

 

Ok, that sounds interesting. I don't know much about ALM, but hopefully we can sort something out.

 

So a common pattern of data-driven testing in SoapUI can be:

 

1. Read line of CSV data or potentially a soap request (using Groovy) in your case. You can do this in a Groovy TestStep. Say you have read the values in you can set them as properties on the context holder e.g.

context["intA"]=2

context["intB"]=3

 

2. Setup a SOAP Request TestStep and use the properties from the context holder to parameterise the SOAP request. You can esily insert the values using whats called property expansions (see http://www.soapui.org/scripting---properties/property-expansion.html) e.g.

 

<?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>${intA}</tem:intA>
<tem:intB>${intB}</tem:intB>
</tem:Add>
</soap:Body>
</soap:Envelope>

 

2a) You can also use the data values from the context to parameterise Assertions e.g. XPath Assertion (expected value) or Content or Script Assertions.

 

3. Use another Groovy TestStep (there is also a Conditional Goto TestStep) to loop back to 1 if there are more rows of test data.

 

Of course if you want to dump the SOAP request/response into a file, the thats easy enough to add with Groovy.

 

Does this sound at all like what you wanted? It just sounded like it might be easier to work with data values e.g. CSV rather than having to manipulate XML with XPaths..

 

If this is not really what you need, we can get back to the plan A and I'll have a go at adapting that Groovy script to use XPaths instead.

 

Cheers,

Rup

 

 

Author of SoapUI Cookbook
Highlighted
New Contributor

Re: How to update tag value in external xml using groovy script?

Hi Rupert,

 

I am also facing a similar problem but the difference is that I am not doing SOAPUI testing but I need to update the SOAPUI response tag values from an excel before forwarding that response to another system ,since my main application is developed in groovy ,I was thinking if this can be done in groovy only.

So my excel header are actually XPATHS and the corresponding rows below contain the value.

any help would be great.

 

Valued Contributor

Re: How to update tag value in external xml using groovy script?

Hi,

 

Ok, I am not sure I get exactly what you mean, please can you explain more? So you say you arent testing with SoapUI, but want to update tags in the response - are you calling some kind of service that returns XML and wanting to update the response based on values taken from an Excel spreadsheet?

 

Thanks,

Rup

Author of SoapUI Cookbook
New Contributor

Re: How to update tag value in external xml using groovy script?

Hi Rupert,

 

Yeah ,so I have an inhouse built(java-groovy) application which generates a SOAP response and I have to feed that response to a proprietory software .But before sending the response to the software I need to update few tag values as per the Excel received from third party.

For now I have manually updated the third party Excel column headers with XPATHs of the XML so that I can update the XMLs as per the values in the corresponding row.

My main problem is reading the column header and passing its value in place of 

Envelope.Body.CamCommand.CamAction.tCANOTIFICATIONS.pCASECIDUNDL

in the below code 

 

String fileContents = new File(FilePath).text
def xmlfromFile = new File(FilePath)
def Envelope = new XmlSlurper().parseText(xmlfromFile.getText())

Envelope.Body.CamCommand.CamAction.tCANOTIFICATIONS.pCASECIDUNDL = "NewData123"

XmlUtil xmlUtil = new XmlUtil()
println xmlUtil.serialize(Envelope)
xmlUtil.serialize(Envelope, new FileWriter(xmlfromFile))

Thanks,

Amitav

 

 

New Member

Re: How to update tag value in external xml using groovy script?

The below link is having an example, Hope this will help

https://github.com/Vignesh-Sakkarai/ParseAndUpdateXML/blob/master/MainXMLParser.groovy

New Here?
Join us and watch the welcome video:
Top Kudoed Authors