Forum Discussion

Patil86's avatar
Patil86
Occasional Contributor
12 years ago

How to remove empty nodes

Hi

I am new to this soap UI and Groovy scripting , i have a situation where i have to check if any of the nodes in the soap request are empty and remove such nodes , i have to traverse the through all the nodes & child nodes to find and remove empty tags .

My request will look something like

<soapenv:Body>
<ns:asd>09123</ns:asd>
<ns:aer>this is one<ns:/aer>
<ns:ent>
<ns:ent1>444.15</ns:ent1>
<ns:entas>ds</ns:entas>
<ns:went>12312356</ns:went>
<ns:wen>201312</ns:wen>
</ns:ent>
<ns:try>
<ns:trya>
<ns:ttr/>
<ns:trr/>
</ns:trya>
<ns:tryb>
<ns:tyy>
<ns:tyyr/>
<ns:tyyt/>
</ns:tyy>
<ns:tryba>
<ns:trycd/>
<ns:tryce/>
</ns:tryba>
<ns:tryb>
</ns:try>
</soapenv:Body>


there can be many nested nodes which i have to remove . Can anyone plz help me through this ...

Regards
Patil
  • ashchow's avatar
    ashchow
    Occasional Contributor
    Add context.RemoveEmptyRecursive = true to the Setup Script of the TestCase
  • Patil86's avatar
    Patil86
    Occasional Contributor
    Thanks for the response ashchow ,
    I followed your suggested step, but i dont know why that is not working for me , so i was trying with below groovy....

    def holder = groovyUtils.getXmlHolder( TStp.testRequest.requestContent )
    use (groovy.xml.dom.DOMCategory) {
    for( node2 in holder.getDomNodes( "//soapenv:Body" )) {
    node2.depthFirst().each { child ->
    if(child.text()==""){
    removeList.add(child.nodeName)
    }
    }
    for(node in removeList){
    holder.removeDomNodes("//"+node)
    }
    }
    }

    This removes the nodes which are not mandatory , but will not remove the nodes which are empty and mandatory , to remove empty mandatory i have run this script multiple time ,is is possible to some how this code is tuned to remove all the nodes irrespective of mandatory or not in single shot

    Regards
    Patil86
  • ashchow's avatar
    ashchow
    Occasional Contributor
    I used a combination of what I suggested above along with setting The TestRequestProperty - 'Remove Empty Content' to true.
    also Just to assert that what I suggested earlier should be in setup script at the test case level. I read complaints about that not working at setup script for Project or at Test suite.