Forum Discussion

AAB45Old's avatar
AAB45Old
New Member
1 hour ago

Change value from attached xml file

Hi SmartBear,

Long time ago that I was here (under a different name :-)  ). Glad to be back though ;-)

So, my question:

Together with a colleague we're creating a POC, composite, where we add a test case. In that testcase we'll add an xml file as 'mutlipart/form-data' that will be send in a query and this is cashed so that everyone can reach the file. 

Now, after sending the REST request, I would like to be able to read the content of the xml in the RAW of the request(because that's the only place where I can see the content of the file) and change a tag value in it. (see screenshot Question-1). e.g.

<vinnumber>8976413164316165</vinnumber>  should change in <vinnumber>B4598L2482255256</vinnumber>

I've tried with Property transfer but I can't point to the Property transfer "Target" section as this is script and the Property Transfer itself doesn't know where to put that value. so I don't think this is the best option. (see screenshot prop transf)

 The groovy script that I've tried is not working neither. It gives me a 'Content is not allowed in prolog: line 1'( importing groovy.xml.XmlUtil)

import groovy.xml.XmlUtil
import groovy.xml.XmlSlurper
import groovy.util.Node
 
// Parse the content of the document and store it in the 'file' variable
def file  =  context.expand( '${POST TM005_SHP STELLANT#RawRequest}' )
log.info file

def xml = new XmlSlurper().parse(file)
log.info xml

// 1. Generate the VIN
def chars = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"
def random = new Random()
def generatedVin = (1..17).collect { chars[random.nextInt(chars.length())] }.join()
log.info "Generated VIN: " + generatedVin

// Change the attribute value for 'vin'
xml.VehicleTransportOrder.Line.Vehicle.VehicleID = $"generatedVin"
  
// Construct a FileWriter object and assigns it to the variable 'writer'
def writer = new FileWriter("C://Documents/vinnumber.xml")
// Write the changes to the file
XmlUtil.serialize(xml, writer)
// Close file
writer.close()

 

I have no clue how I could execute this wish.

Does someone have an idea please?

Thanks in advance, AAB

 

No RepliesBe the first to reply