Forum Discussion

jays's avatar
jays
Occasional Contributor
8 years ago
Solved

How to add XML node dynamically with a namespace

I have an optional xml node which I need to add as I am doing data driven testing. I want to add it based on the input and it has a namespace as well. I was able to add the node dynamically, but namespace is not getting added. Please help me in adding the namespace to that.

 

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

holder = groovyUtils.getXmlHolder( "SOAP Request#Request"

def parentnode = holder.getDomNode("//v1:GetNames")

XmlUtils.addChildElement(parentnode, "v1:firstname", "abc")

holder.updateProperty()

 

Even though I'm adding "v1:firstname", request xml shows only "firstname" and namespace is not added.

  • Found the answer. I had to use the 'appendChild' instead of 'addChildElement'.

     

    def parentnode = holder.getDomNode("//v1:GetNames")
    def xmldoc = parentnode.ownerDocument
    def ns = "your namespace address goes here"

    def child = parentnode.appendChild(xmldoc.createElementNS(ns, "Name"))
    child.appendChild(xmldoc.createTextNode("abc"))

1 Reply

  • jays's avatar
    jays
    Occasional Contributor

    Found the answer. I had to use the 'appendChild' instead of 'addChildElement'.

     

    def parentnode = holder.getDomNode("//v1:GetNames")
    def xmldoc = parentnode.ownerDocument
    def ns = "your namespace address goes here"

    def child = parentnode.appendChild(xmldoc.createElementNS(ns, "Name"))
    child.appendChild(xmldoc.createTextNode("abc"))