Eviware Forum

soapUI => soapUI => Topic started by: Akarui Tomodachi on May 26, 2008, 07:41:39 am



Title: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: Akarui Tomodachi on May 26, 2008, 07:41:39 am
PLEASE HELP !!!!

How to append (insert) element repetition in the default request file ?
 
I have a "default request" as below:
 
/******************* Default request before repeat ***************************
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://xyz.ca/ebusiness/ProductServiceWrapper"
xmlns:prod1="http://xyz.ca/ebusiness/ProductService">
<soapenv:Header/>
<soapenv:Body>
<prod:getDistProductDetails>
<prod:request>
<!--1 or more repetitions:-->
<prod1:ProdSet DestCountryCode="?" ContractNumber="?">
<!--Zero or more repetitions:-->
<prod1:ProductServiceId>?</prod1:ProductServiceId>
</prod1:ProdSet>
</prod:request>
</prod:getDistProductDetails>
</soapenv:Body>
</soapenv:Envelope>
****************************************************************************/
 
 
Now, I need to insert (repeatedly) the element "ProductServiceId"  3 times which will look like:
 
/******************* Default request after repeat inserted *****************
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://xyz.ca/ebusiness/ProductServiceWrapper" 
xmlns:prod1="http://xyz.ca/ebusiness/ProductService">
<soapenv:Header/>
<soapenv:Body>
<prod:getDistProductDetails>
<prod:request>
<!--1 or more repetitions:-->
<prod1:ProdSet DestCountryCode="?" ContractNumber="?">
<!--Zero or more repetitions:-->
<prod1:ProductServiceId>?</prod1:ProductServiceId>
<prod1:ProductServiceId>?</prod1:ProductServiceId>
<prod1:ProductServiceId>?</prod1:ProductServiceId>

</prod1:ProdSet>
</prod:request>
</prod:getDistProductDetails>
</soapenv:Body>
</soapenv:Envelope>
****************************************************************************/
 
How can I do it in Groovy ?
I have tried as below, but it is giving me an exception (I know, did something wrong  >:( )

"Mon May 26 02:32:18 EDT 2008:ERROR:groovy.lang.MissingMethodException: No signature of method: groovy.util.NodeList.appendNode() is applicable for argument types: (java.lang.String, java.lang.String) values: {"prod1:ProductServiceId", "?"}"

My code snippet is:
/********************
...
...
def root = new XmlParser().parseText(newRequest)

root["soapenv:Body"]["prod:getDistProductDetails"]["prod:request"]["prod1:ProdSet"].appendNode("prod1:ProductServiceId", "?")

def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(root)
def result = writer.toString()
...
...
********************/
 


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: omatzura on May 26, 2008, 09:46:57 am
Hi!

Try using GroovyUtils/XmlHolder to get the DOM node of the parent ("prod1:request"), and then use the DOMCategory to append the nodes.. something in the line of
Code:
import groovy.xml.dom.DOMCategory
import groovy.xml.QName

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

// get the xml to update
def holder = groovyUtils.getXmlHolder( "Request 1#Request" )

// get parent node for new child
def node = holder.getDomNode( "//prod1:request" )

// use DOMCategory to simplify things..
use( DOMCategory )
{
   // add new "ProductServiceId" element with "?" value..
   node.appendNode( new QName( "http://xyz.ca/ebusiness/ProductService", "ProductServiceId", "prod1"), "?" ) }

holder.updateProperty()

Hope this helps!

regards,

/Ole
eviware.com


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: Akarui Tomodachi on May 27, 2008, 07:24:16 am
Hi Ole:
Thanks for your help.
Your code sample worked like a magic.

But I am still having trouble to insert a node with attributes.

For example, below is the XML before inserting node with attributes (in bold):
/******************
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://xyz.ca/ebusiness/ProductServiceWrapper" xmlns:prod1="http://xyz.ca/ebusiness/ProductService">
   <soapenv:Header/>
   <soapenv:Body>
      <prod:getDistProductDetails>
         <prod:request>
            <!--1 or more repetitions:-->
            <prod1:ProdSet DestCountryCode="?" ContractNumber="?">
               <!--Zero or more repetitions:-->
               <prod1:ProductServiceId>?</prod1:ProductServiceId>
               <prod1:ProductServiceId>?</prod1:ProductServiceId>
            </prod1:ProdSet>
         </prod:request>
      </prod:getDistProductDetails>
   </soapenv:Body>
</soapenv:Envelope>
*******************/

Now, I like to append the node with attributes and it should look like as below:
/****************
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://innovapost.ca/ebusiness/ProductServiceWrapper" xmlns:prod1="http://innovapost.ca/ebusiness/ProductService">
   <soapenv:Header/>
   <soapenv:Body>
      <prod:getDistProductDetails>
         <prod:request>
            <!--1 or more repetitions:-->
            <prod1:ProdSet DestCountryCode="?" ContractNumber="?">
            <prod1:ProdSet DestCountryCode="?" ContractNumber="?">

               <!--Zero or more repetitions:-->
               <prod1:ProductServiceId>?</prod1:ProductServiceId>
               <prod1:ProductServiceId>?</prod1:ProductServiceId>
            </prod1:ProdSet>
         </prod:request>
      </prod:getDistProductDetails>
   </soapenv:Body>
</soapenv:Envelope>
******************/

I tried with many ways after changing the code under the use (DOMCategory) {} but nothing is working. Please give me some hints.

Thanks in advance.


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: omatzura on May 27, 2008, 08:47:39 am
Hi!

hm.. should the ProdSet node have no content initially? And should it be inserted as the first child of the request?

regards,

/Ole
eviware.com


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: Akarui Tomodachi on May 27, 2008, 02:57:39 pm
Hi Ole:
Thanks for your kind attention into this matter at your busy schedule.
Anyway, actually ProdSet has got the default attributes from the beginning. Here is the default request:

/**************
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://xyz.ca/ebusiness/ProductServiceWrapper" xmlns:prod1="http://xyz.ca/ebusiness/ProductService">
   <soapenv:Header/>
   <soapenv:Body>
      <prod:getDistProductDetails>
         <prod:request>
            <!--1 or more repetitions:-->
            <prod1:ProdSet DestCountryCode="?" ContractNumber="?">
      <!--Zero or more repetitions:-->
               <prod1:ProductServiceId>?</prod1:ProductServiceId>
            </prod1:ProdSet>
         </prod:request>
      </prod:getDistProductDetails>
   </soapenv:Body>
</soapenv:Envelope>
**************/


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: omatzura on May 28, 2008, 08:50:09 pm
Hi,

sorry, I'm still not exactly sure of what you want to do here.. do you want to add a new ProdSet and contained ProductServiceId elements? Or modify the existing one in the default message? (including the attributes)

regards!

/Ole
eviware.com


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: Akarui Tomodachi on May 29, 2008, 03:44:27 am
Hi Ole:
I am sorry for not explaining the problem properly, my apology  :'(
Actually, the comment line ("<!--1 or more repetitions:-->") before the element "<prod1:ProdSet" is a bit confusing.

You are most likely right (and it looks like obvious!) that the repeat of the node "<prod1:ProdSet" will include its two attributes DestCountryCode= ContractNumber= and child node "<prod1:ProductServiceId>" as well. So when I'll repeat the node "prod1:ProdSet" twice, the default request file should look like as below:

I'll confirm this with designer of the WSDL and let you know.
Thanks.

/**************
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://xyz.ca/ebusiness/ProductServiceWrapper" xmlns:prod1="http://xyz.ca/ebusiness/ProductService">
   <soapenv:Header/>
   <soapenv:Body>
      <prod:getDistProductDetails>
         <prod:request>

            <!--1 or more repetitions:-->

            <prod1:ProdSet DestCountryCode="?" ContractNumber="?">
               <!--Zero or more repetitions:-->
               <prod1:ProductServiceId>?</prod1:ProductServiceId>
            </prod1:ProdSet>

            <prod1:ProdSet DestCountryCode="?" ContractNumber="?">
               <!--Zero or more repetitions:-->
               <prod1:ProductServiceId>?</prod1:ProductServiceId>
            </prod1:ProdSet>

         </prod:request>
      </prod:getDistProductDetails>
   </soapenv:Body>
</soapenv:Envelope>
**************/



Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: omatzura on May 29, 2008, 08:29:28 am
Hi!

ok.. so the following code will add a new ProdSet with default attributes and a contained ProductServiceId:
Code:
import groovy.xml.dom.DOMCategory
import groovy.xml.QName

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

// get the xml to update
def holder = groovyUtils.getXmlHolder( "Request 1#Request" )

// get parent node for new child
def node = holder.getDomNode( "//prod1:request" )

// use DOMCategory to simplify things..
use( DOMCategory )
{
   def ns = "http://xyz.ca/ebusiness/ProductService"

   // add new "ProdSet" element..
   def prodSet = node.appendNode( new QName( ns, "ProdSet", "prod1"))

   // set its attributes
  prodSet.setAttribute( "DestCountryCode", "?" )
  prodSet.setAttribute( "ContractNumber", "?" )

  // add new "ProductServiceId" element with "?" value..
   prodSet.appendNode( new QName( ns, "ProductServiceId", "prod1"), "?" ) }

}

holder.updateProperty()

ok?

regards!

/Ole
eviware.com


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: Akarui Tomodachi on May 29, 2008, 08:49:12 pm
Hi Ole:
Thanks for your help.
I works exactly the same way I wanted.
Best Regards.

/atomodachi


Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: Maria Aschauer on July 10, 2008, 09:26:30 am
hi,

I am facing a similar task. I tried your solution, but it still doesn't work:

Code:
// get parent node for new child
def node = holder.getDomNode( "//prod1:request" )


the type of node is org.apache.xmlbeans.impl.store.Xobj$ElementXobj and not as expected of org.w3c.dom.Node...



Title: Re: PLEASE HELP !!!: How to append "repetition" of nodes ?
Post by: omatzura on July 10, 2008, 10:00:59 pm
Hi,

this type implements the org.w3c.dom.Element interface (which extends org.w3c.dom.Node), so you should be able to use it as such..

regards,

/Ole
eviware.com