Forum Discussion

AngieD18's avatar
AngieD18
Contributor
9 years ago

Using Groovy to insert new tag into request. Need help with insert point (XmlParser - FindAll)

Hi. I have a request that I want to change by inserting a new tag after an existing tag.  I am using Groovy code. I have my code working except for one little thing - I can't find where to put the ne...
  • AngieD18's avatar
    AngieD18
    9 years ago

    Again, this isn't pretty.

     

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = context.expand( '${SOAPRequest#Request}' )
    def root = new XmlParser( false, true ).parseText( holder )

     

    fragmentToAdd = new XmlParser( false, true ).parseText( toAdd )  //Node to be added in the format <tag>Value</tag>

      root.'**'.findAll() {

        //The new node will be added after rootNode
        if ( it.name().toString().contains( rootNode.substring(2,rootNode.length()) )) { 
           int nodeCount = 0
           def insertPoint = it.parent().children().find { 
              nodeCount++

              // Find the node in the list of nodes
              it.name().toString().contains( rootNode.substring(2,rootNode.length()))
           }
        it.parent().children().add( nodeCount , fragmentToAdd ) //Add new node at the specified point.
      }
    }
    String outxml = groovy.xml.XmlUtil.serialize( root )

    request = testRunner.testCase.getTestStepByName( "SOAPRequest" )
    request.getProperty("request").setValue( outxml )

     

    Angie