Forum Discussion

Akarui_Tomodach's avatar
Akarui_Tomodach
Occasional Contributor
16 years ago

PLEASE HELP !!!: How to append "repetition" of nodes ?

PLEASE HELP !!!!

How to append (insert) element repetition in the default request file ?

I have a "default request" as below:

/******************* Default request before repeat ***************************

xmlns:prod1="http://xyz.ca/ebusiness/ProductService">







?





****************************************************************************/


Now, I need to insert (repeatedly) the element "ProductServiceId"  3 times which will look like:

/******************* Default request after repeat inserted *****************
xmlns:prod1="http://xyz.ca/ebusiness/ProductService">







?
?
?






****************************************************************************/

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()
...
...
********************/

10 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    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

    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
  • Akarui_Tomodach's avatar
    Akarui_Tomodach
    Occasional Contributor
    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):
    /******************

     

     
         
           
               
               
                 
                  ?
                  ?
               

           

         
     


    *******************/

    Now, I like to append the node with attributes and it should look like as below:
    /****************

     

     
         
           
               
               
               

                 
                  ?
                  ?
               

           

         
     


    ******************/

    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.
  • omatzura's avatar
    omatzura
    Super Contributor
    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
  • Akarui_Tomodach's avatar
    Akarui_Tomodach
    Occasional Contributor
    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:

    /**************

     

     
         
           
               
               

                  ?
               

           

         
     


    **************/
  • omatzura's avatar
    omatzura
    Super Contributor
    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
  • Akarui_Tomodach's avatar
    Akarui_Tomodach
    Occasional Contributor
    Hi Ole:
    I am sorry for not explaining the problem properly, my apology 
    Actually, the comment line ("") before the element " is a bit confusing.

    You are most likely right (and it looks like obvious!) that the repeat of the node " will include its two attributes DestCountryCode= ContractNumber= and child node "" 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.

    /**************

     

     
         
           

               

               
                 
                  ?
               


               
                 
                  ?
               


           

         

     

    **************/
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    ok.. so the following code will add a new ProdSet with default attributes and a contained ProductServiceId:

    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
  • Akarui_Tomodach's avatar
    Akarui_Tomodach
    Occasional Contributor
    Hi Ole:
    Thanks for your help.
    I works exactly the same way I wanted.
    Best Regards.

    /atomodachi
  • hi,

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


    // 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...
  • omatzura's avatar
    omatzura
    Super Contributor
    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