Forum Discussion

Clear_Capital__'s avatar
Clear_Capital__
Contributor
12 years ago

groovy - remove first and last line of a xml

def ResponseXML = context.expand( '${myRESTtestStep#ResponseAsXml#//Response[1]/e[1]}' )

gives me

<e>
<distance>0.44</distance>
<id>123456</id>
<value>
<Count1>2</Count1 >
<Count2>3</Count2>
</value>
</e>

I need to be able to remove first and lines, <e> and </e> , such that I get following value

<distance>0.44</distance>
<id>123456</id>
<value>
<Count1>2</Count1 >
<Count2>3</Count2>
</value>

Please let me know how would I do that ?

Thank you

3 Replies

  • Hello,

    Please keep in mind that Groovy scripting is outside of our support agreement since we have limited resources and issues can become very complex. I will make an exception, please see script below that will remove the e tag. Let me know if you have further questions or issues

    def grUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def ResponseHolder = grUtils.getXmlHolder("Request name#Response")
    ResponseHolder.removeDomNodes('XPath to <e node>')
    ResponseHolder.updateProperty()

    Regards,
    Temil Sanchez
  • unfortunately the code you suggesting runs without error , but its not removing the node.
  • Hi,

    I would suggest you to try this in RequestFilter.afterRequest event Handler,

    http://www.soapui.org/Scripting-Properties/custom-event-handlers.html

    Paste the code in

    def grUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def content = context.httpResponse.getContentAsXml()
    def ResponseHolder = grUtils.getXmlHolder(content)
    content = ResponseHolder.getXml().toString()
    content = content.replaceFirst("<e>", "")
    content = content.replaceAll("\n </e>", "")


    content = content.stripMargin()


    context.httpResponse.setResponseContent(content)


    Thanks,
    Jeshtha