Forum Discussion

JP's avatar
JP
New Contributor
16 years ago

Remove empty child and parent tags

I'm looking to find a way to remove empty child & parent tags from a request. Whilst the option 'Remove empty content' gets rid of child elements, the parent element remains in the request.

Having looked around on the forum, I've followed the suggestion of using a request filter, and have managed to get an example filter working (shown below).

Can anyone help me to amend the filter so that it removes empty child & parent tags?

Any help would be appriciated...
---------------------------------
package soapui.demo

import com.eviware.soapui.SoapUI
import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport
import com.eviware.soapui.impl.wsdl.submit.RequestFilter
import com.eviware.soapui.impl.wsdl.WsdlRequest
import com.eviware.soapui.model.iface.Request
import com.eviware.soapui.model.iface.Response
import com.eviware.soapui.model.iface.SubmitContext

public class DemoRequestFilter implements RequestFilter
{
public void afterRequest( SubmitContext context, Response response )
{}
public void filterRequest( SubmitContext context, WsdlRequest wsdlRequest )

String content = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT ) 
if( content == null )
{
System.err.println( "Missing request content in context, skipping demofilter" )
}
else
{
System.out.println( "running demofilter" )
content = content.replaceAll( "<(.+)>--remove--", "" )
context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, content )
}
}
}

1 Reply

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    Try parsing the string to a DOM Document, then traverse it using DOM Traversal APIs removing empty elements as you go and then serialize the result back to a string:

    // get DOM Document
    Document dom = XmlUtils.parse( content );

    // traverse as documented at http://www.cafeconleche.org/books/xmljava/chapters/ch12.html
    ...

    // serialize back to string
    StringWriter writer = new StringWriter();
    XmlUtils.serialize( dom, writer );

    // save in context
    context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, writer.toString() );


    Hope this helps!

    regards,

    /Ole
    eviware.com