Forum Discussion

NYefimov's avatar
NYefimov
Occasional Contributor
18 years ago

modify xml

Hey guys,

let's say I want to get rid of 2 nodes in my XML request.
Node names are: "lis:PGID", "lis:TVObjectID"

XML request is below:

 

 
     
       
       
           
            gero et
           
            sonoras imperio
           
           
            quae divum incedo
           
            verrantque per auras
           
            per auras
           
            <metadata>Keyword

           
            3
           
            3
           
            PlainText
           
            nimborum in
           
            2013-12-21T03:32:42-08:00
           
            3
           
            false
           
           
             
             
                 
                  et carcere
             

              HD
             
             
                 
                  Music
             

           

       

     

 


How the groovy script should look like?

Thanks

7 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    you can use GroovyUtils / XmlHolder for this:

    [tt:35qvqpgl]def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = groovyUtils.getXmlHolder( "TestRequest#Request" )

    def node = holder.getDomNode( "//lis:PGID" )
    node.parentNode.removeChild( node )

    node = holder.getDomNode( "//lis:TVObjectID" )
    node.parentNode.removeChild( node )

    holder.updateProperty()[/tt:35qvqpgl]

    Hope this helps!

    regards,

    /Ole
    eviware.com
  • NYefimov's avatar
    NYefimov
    Occasional Contributor
    Ole,

    it works. thanks
    just 1 more question:

    def node = holder.getDomNode( "//lis:PGID" )
    node is of class org.w3c.dom.Node
    there are methods in this class
    getParentNode() and removeChild(Node)

    node.parentNode.removeChild( node )

    you use node.parentNode
    like you you are accessing a public field parentNode
    I did not find parentNode as a field in the class class org.w3c.dom.Node
    Where does it come from?

    thanks in advance
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    it's groovys way of accessing properties, ie a obj.getXXXX() method can in groovy be called with just obj.xXXXX

    regards!

    /Ole
    eviware.com
  • Beno_Iskratel's avatar
    Beno_Iskratel
    Frequent Contributor
    hi,

    i have a question about modifying XML as well...

    that's some response:

    <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
    <getDestinationCriteriaResponse xmlns="urn:iskratel-si:itnbsp-1-0">
    <node node="42308" destinationId="2" xmlns="">
    <param value="1" key="criteriaType1st"/>
    <param value="1" key="criteriaType2nd"/>
    <param value="1" key="destinationType"/>
    <param value="1" key="carrierReq"/>
    <param value="333" key="waitAnswerTime"/>
    </node>
    </getDestinationCriteriaResponse>
    </soapenv:Body>
    </soapenv:Envelope>


    from this response i would like to take whole <node>...</node> and paste it into another request.

    here is what i do:

    String response_to_paste = context.expand( '${getDestinationCriteria - prepair#Response#declare namespace ns1=\'urn:iskratel-si:itnbsp-1-0\'; //ns1:getDestinationCriteriaResponse[1]/node[1]}' )

    this gives me:

    <node node="42308" destinationId="2">
    <param value="1" key="criteriaType1st"/>
    <param value="1" key="criteriaType2nd"/>
    <param value="1" key="destinationType"/>
    <param value="1" key="carrierReq"/>
    <param value="333" key="waitAnswerTime"/>
    </node>


    than i choose another request where i want to paste response_to_paste.
    def holder1 = groovyUtils.getXmlHolder( "modifyDestinationCriteria - repair#Request" )

    i remove <node>...</node> from this request to make space for new one.
    holder1.remove("//urn:modifyDestinationCriteriaRequest[1]/node[1]")

    if i choose put or setNodeValue it puts my extracted response_to_paste into request, ok...
    holder1.put( "//urn:modifyDestinationCriteriaRequest[1]", response)
    holder1.updateProperty()

    ...but, what it makes is:

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:iskratel-si:itnbsp-1-0">
    <soap:Header/>
    <soap:Body>
    <urn:modifyDestinationCriteriaRequest><![CDATA[<node node="42308" destinationId="2">
    <param value="1" key="criteriaType1st"/>
    <param value="1" key="criteriaType2nd"/>
    <param value="1" key="destinationType"/>
    <param value="1" key="carrierReq"/>
    <param value="333" key="waitAnswerTime"/>
    </node>]]></urn:modifyDestinationCriteriaRequest>
    </soap:Body>
    </soap:Envelope>


    CDATA!!!
    what shall i use to get rid of this?

    tnx,
    beno
  • Hello,

    This sounds like something which is much easier done using a Property Transfer. Just create a Property Transfer TestStep somewhere inbetween the two requests (or if you already have one, you can use that) and add a transfer, which transfers the source "node" element to the target element. Be sure to check the "Transfer Child Nodes" checkbox!

    When done, the source and target fileds should have something similar to:


    declare namespace ns1='urn:iskratel-si:itnbsp-1-0';
    //ns1:getDestinationCriteriaResponse[1]



    declare namespace ns1='urn:iskratel-si:itnbsp-1-0';
    //urn:modifyDestinationCriteriaRequest[1]


    Regards,
    Dain
    eviware.com
  • Beno_Iskratel's avatar
    Beno_Iskratel
    Frequent Contributor
    tnx,

    i know this.
    that's how i did and it's working great.
    it's just that i was so close with groovy also... it almost works.
    i think i'll still try to make this work but it's ok, property transfer is all i need right now.
    CDATA is probably logical thing since i inserted simple string and not node with childs and stuff...

    regards,
    beno