Forum Discussion

praneeth12's avatar
praneeth12
Occasional Contributor
5 years ago

How to add nodes for soapUI request,useing groovy?

Actual Requset.

<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
	<actors>
		<actor>Christian Bale</actor>
		<actor>Liam Neeson</actor>
		<actor>Michael Caine</actor>
	</actors>
	<foo:singers>
		<foo:singer>Tom Waits</foo:singer>
		<foo:singer>B.B. King</foo:singer>
		<foo:singer>Ray Charles</foo:singer>
	</foo:singers>
</root>

Expected request

<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
	<actors>
		<actor >Christian Bale</actor>
		<actor >Liam Neeson</actor>
		<actor >Michael Caine</actor>
	</actors>
	<foo:singers>
		<foo:singer>Tom Waits</foo:singer>
		<foo:singer>B.B. King</foo:singer>
		<foo:singer>Ray Charles</foo:singer>
	</foo:singers>
<foo:singers>
<foo:singer >Tom Waits</foo:singer>
<foo:singer >Ray Charles</foo:singer>
</foo:singers> 
</root>

 

    • praneeth12's avatar
      praneeth12
      Occasional Contributor

      Thank you for your reply.

       

      I tried with below code

      addNewNode = """         """" // new node in parseText format 

      def root8 = new XmlParser().parseText( requestXML ) // converted request xml into parseText


      for (item in root8){
      for(test1 in item){
      nodelist = test1.value()
      nodelist.add(addNewNode)
      }
      }
      log.info root8
      String outxml = groovy.xml.XmlUtil.serialize(root8)

      I am able to append node but the problem was when i am serializing  data in xml format, new node is on parsetext format.How to serialize new node.

      Any one help me on this..

  • praneeth12's avatar
    praneeth12
    Occasional Contributor

     

    Expected result

    <soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
    soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
    	<soap:Header>
    		<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
      soap:actor="https://www.w3schools.com/code/">234
    		</m:Trans>
    	</soap:Header>
    	<soap:Body>
    		<CATALOG>
    			<CD>
    				<TITLE>Empire Burlesque</TITLE>
    				<ARTIST>Bob Dylan</ARTIST>
    				<PRICE>10.90</PRICE>
    			</CD>
    			<CD>
    				<TITLE>Hide your heart</TITLE>
    				<ARTIST>Bonnie Tyler</ARTIST>
    				<PRICE>9.90</PRICE>
    			</CD>
    			
    		</CATALOG>
    	</soap:Body>
    </soap:Envelope>

     

     

     

     

     

     

    Below is requst

     

     

    <soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
    soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
    	<soap:Header>
    		<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
      soap:actor="https://www.w3schools.com/code/">234
    		</m:Trans>
    	</soap:Header>
    	<soap:Body>
    		<CATALOG>
    			<CD>
    				<TITLE>Empire Burlesque</TITLE>
    				<ARTIST>Bob Dylan</ARTIST>
    				<PRICE>10.90</PRICE>
    			</CD>
    			<CD>
    				<TITLE>Hide your heart</TITLE>
    				<ARTIST>Bonnie Tyler</ARTIST>
    				<PRICE>9.90</PRICE>
    			</CD>
    			<CD>
    				<TITLE>Greatest Hits</TITLE>
    				<ARTIST>Dolly Parton</ARTIST>
    				<PRICE>9.90</PRICE>
    			</CD>
    			<CD>
    				<TITLE>Still got the blues</TITLE>
    				<ARTIST>Gary Moore</ARTIST>
    				<PRICE>10.20</PRICE>
    			</CD>
    		</CATALOG>
    	</soap:Body>
    </soap:Envelope>

     

    • appliedMagic's avatar
      appliedMagic
      Occasional Contributor

      Hi,

      maybe this will help you to get build/append your requests. Since I do not know where you get the data from and how it is provided (represented in the code) I will assume a list of maps, the map contains the CD information.

       

       

       

      //the request body with no CDs
      def reqBody = """<?xml version="1.0" encoding="UTF-8"?>
      <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
        <soap:Header>
          <m:Trans xmlns:m="https://www.w3schools.com/transaction/" soap:actor="https://www.w3schools.com/code/">234</m:Trans>
        </soap:Header>
        <soap:Body>
          <CATALOG/>
        </soap:Body>
      </soap:Envelope>"""
      
      //for our test lets use a list of maps, each map contains one CD
      def cds = [
      	[	"TITLE": "Empire Burlesque",
      		"ARTIST": "Bob Dylan",
      		"PRICE": "10.90"],
      	[	"TITLE": "Hide your heart",
      		"ARTIST": "Bonnie Tyler",
      		"PRICE": "9.90"],
      	[	"TITLE": "Greatest Hits",
      		"ARTIST": "Dolly Parton",
      		"PRICE": "9.90"],
      	[	"TITLE": "Still got the blues",
      		"ARTIST": "Gary Moore",
      		"PRICE": "10.20"]
      ]
      
      //read in the request body and find the catalog node
      def root = new XmlParser().parseText( reqBody )	//<Envelope>
      def catalog = root.'**'.find { it.name().toString().equalsIgnoreCase("catalog") }	//find catalog
      
      //now add all CDs
      for(cd in cds) {
      	//create CD node
      	Node cdNode = new Node(catalog, "CD")
      
      	//add information to CD node
      	for(key in ["TITLE", "ARTIST", "PRICE"]) {
      		cdNode.appendNode(key, cd[key])
      	}
      }
      
      //check result till here
      log.info(groovy.xml.XmlUtil.serialize(root))
      
      //add even more CDs
      def moreCDs = [
      	[	"TITLE": "Greatest Hits Vol.1",
      		"ARTIST": "Various Artists",
      		"PRICE": "14.95"],
      	[	"TITLE": "Greatest Hits Vol.2",
      		"ARTIST": "Various Artists",
      		"PRICE": "17.95"]
      ]
      
      
      // add even more CDs
      for(cd in moreCDs) {
      	//create CD node
      	Node cdNode = new Node(catalog, "CD")
      
      	//add information to CD node
      	for(key in ["TITLE", "ARTIST", "PRICE"]) {
      		cdNode.appendNode(key, cd[key])
      	}
      }
      
      //convert to text
      def requestXML = groovy.xml.XmlUtil.serialize(root)
      
      //check result
      log.info(requestXML)
      
      log.info("done")

       

       

       

      I used this to lookup a few examples of how to interact with an XML using XMLParser (didn't really (often) use it before):

      (documentation)

      http://docs.groovy-lang.org/latest/html/api/groovy/util/Node.html

      (some examples of how to use them)

      https://www.codota.com/web/assistant/code/rs/5c6574441095a5000151d7bf#L241

       

      We started with:

      <?xml version="1.0" encoding="UTF-8"?>
      <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
        <soap:Header>
          <m:Trans xmlns:m="https://www.w3schools.com/transaction/" soap:actor="https://www.w3schools.com/code/">234</m:Trans>
        </soap:Header>
        <soap:Body>
          <CATALOG/>
        </soap:Body>
      </soap:Envelope>

       

      The result, after we run the code, looks like this:

      <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
        <soap:Header>
          <m:Trans xmlns:m="https://www.w3schools.com/transaction/" soap:actor="https://www.w3schools.com/code/">234</m:Trans>
        </soap:Header>
        <soap:Body>
          <CATALOG>
            <CD>
              <TITLE>Empire Burlesque</TITLE>
              <ARTIST>Bob Dylan</ARTIST>
              <PRICE>10.90</PRICE>
            </CD>
            <CD>
              <TITLE>Hide your heart</TITLE>
              <ARTIST>Bonnie Tyler</ARTIST>
              <PRICE>9.90</PRICE>
            </CD>
            <CD>
              <TITLE>Greatest Hits</TITLE>
              <ARTIST>Dolly Parton</ARTIST>
              <PRICE>9.90</PRICE>
            </CD>
            <CD>
              <TITLE>Still got the blues</TITLE>
              <ARTIST>Gary Moore</ARTIST>
              <PRICE>10.20</PRICE>
            </CD>
            <CD>
              <TITLE>Greatest Hits Vol.1</TITLE>
              <ARTIST>Various Artists</ARTIST>
              <PRICE>14.95</PRICE>
            </CD>
            <CD>
              <TITLE>Greatest Hits Vol.2</TITLE>
              <ARTIST>Various Artists</ARTIST>
              <PRICE>17.95</PRICE>
            </CD>
          </CATALOG>
        </soap:Body>
      </soap:Envelope>