rest - Read and temporarily hold XML content from a file in Groovy -


i'm trying parse xml file has multiple contacts of groovy. want each contact file in xml , send part of rest request, 1 one. tried writing groovy script of xmlparser xmlslurper, haven't been successful in doing so. i'm new groovy.

here contents of file:

<contacts>     <contact>         <firstname>dummy</firstname>         <lastname>contact1</lastname>         <gender>m</gender>     </contact>      <contact>         <firstname>dummy</firstname>         <lastname>contact2</lastname>         <gender>f</gender>     </contact> </contacts> 

here attempt @ code using xmlslurper:

import groovy.xml.*  //get file path def groovyutils = new com.eviware.soapui.support.groovyutils(context) def projectpath = groovyutils.projectpath def f = new file(projectpath + '\\contacts - xml.txt')  //check if file exists if(!f.exists()) {     throw exception("the xml file not exist")   }  //parse file def xml = new xmlslurper().parse(f) log.info xml 

the following output above code:

dummycontact1mdummycontact2f 

i can access invidual nodes by, example:

log.info xml.contact[0].gender 

also when use xmlparser, instead of xmlslurper, response seems more unusable:

contacts[attributes={}; value=[contact[attributes={}; value=[firstname[attributes={}; value=[dummy]], lastname[attributes={}; value=[contact1]], gender[attributes={}; value=[m]]]], contact[attributes={}; value=[firstname[attributes={}; value=[dummy]], lastname[attributes={}; value=[contact2]], gender[attributes={}; value=[f]]]]]] 

is there way can read content file xml , store in variable holds xml, can pass rest post request? there's no way of knowing number of contact records in file or number of nodes per contact.

you on right track xmlslurper(). continue script:

def contacts = xml.'**'.findall { it.name().equals('contact') } contacts.each {     // contact     log.info it.firstname.text()     log.info it.lastname.text()     log.info it.gender.text() } 

have read through documentation.

you rewrite each contact appropriate request - did not specify want like. store in variable:

testcase.setpropertyvalue('mycontact', mycontact) 

and in request unwrap ${testcase#mycontact}.

even free version of soapui has built-in loops, loop on many times need: until ${testcase#mycontact} becomes empty.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -