Forum Discussion

fenwickr's avatar
fenwickr
Occasional Contributor
10 years ago

Updating Second Child Element

Below is the Code I am using to setup 2 child nodes Selection of Selections- I need to updated the second child node/Element with a New ID and order Number - I have tried to use XmlUtils.getChildElement(parentNode, "Selection") But not sure what to enter in the () or how I can get the second Child Element to update 

 

 

def reqHolder = groovyUtils.getXmlHolder( "$teststepname#Request" )


parentNode = reqHolder.getDomNode("//Selections")
XmlUtils.addChildElement(parentNode, "Selection", "")
reqHolder.updateProperty()

 

parentNode = reqHolder.getDomNode("//Selections")
XmlUtils.addChildElement(parentNode, "Selection", "")
reqHolder.updateProperty(


//*****Adding attributes***** (In this case, adding an attribute active="true" to the number element
parentNode = reqHolder.getDomNode("//Selection")
parentNode.setAttribute("id", '${#Project#WRunner}')
parentNode.setAttribute("order", "1")
reqHolder.updateProperty()

  • Found the answer put in [] after the element and the number of the node/element you are after 

    So parentNode = reqHolder.getDomNode("//Selection") will become parentNode = reqHolder.getDomNode("//Selection[1]") to update the first element/Node and ("//Selection[2]") for the second

     

    parentNode = reqHolder.getDomNode("//Selection[1]")
    parentNode.setAttribute("id", '${#Project#WRunner1}')
    parentNode.setAttribute("order", "1")
    reqHolder.updateProperty()

     

    parentNode = reqHolder.getDomNode("//Selection[2]")
    parentNode.setAttribute("id", '${#Project#WRunner2}')
    parentNode.setAttribute("order", "2")
    reqHolder.updateProperty()

  • fenwickr's avatar
    fenwickr
    Occasional Contributor

    Found the answer put in [] after the element and the number of the node/element you are after 

    So parentNode = reqHolder.getDomNode("//Selection") will become parentNode = reqHolder.getDomNode("//Selection[1]") to update the first element/Node and ("//Selection[2]") for the second

     

    parentNode = reqHolder.getDomNode("//Selection[1]")
    parentNode.setAttribute("id", '${#Project#WRunner1}')
    parentNode.setAttribute("order", "1")
    reqHolder.updateProperty()

     

    parentNode = reqHolder.getDomNode("//Selection[2]")
    parentNode.setAttribute("id", '${#Project#WRunner2}')
    parentNode.setAttribute("order", "2")
    reqHolder.updateProperty()

    • rlong98's avatar
      rlong98
      Contributor

      Hi - I'm trying to advance my XML learning... curious what you used this code in?  Is this doing some sort of nextVal?

      • fenwickr's avatar
        fenwickr
        Occasional Contributor

        Hi Rlong98 

        It is to setup my requests via a script, as my WSDL set sometime removes the elements when I update them when changes are made to this area, so this way I can make sure they are always the same after an update and use the same values and I will be able to use one request not 80 requests for a smoke test 

         

        It can also be used for Xquery when you have an element with the same name in a response

         

        Found this is a good site to help out 

        http://www.w3schools.com/xsl/xpath_examples.asp