3

I am trying add a new node to the request via SOAPUI Groovy I have the String XMl fragment but I am not able to create a node using Groovy for SOAPUI.

For example

<entityProps>
  <candidate> <id>1</id><key></key> </candidate>
  <candidate> <id>2</id><key></key> </candidate>
  <candidate> <id>3</id><key></key> </candidate>
  <candidate> <id>4</id><key></key> </candidate>
</entityProps>

I want to add new <candidate></candidate> nodes to this request. I already have the string, but I need to convert that into a Document node.

tim_yates
  • 167,322
  • 27
  • 342
  • 338

1 Answers1

4

Given the xml that you currently have:

String doc = '''<entityProps>
               |  <candidate> <id>1</id><key></key> </candidate>
               |  <candidate> <id>2</id><key></key> </candidate>
               |  <candidate> <id>3</id><key></key> </candidate>
               |  <candidate> <id>4</id><key></key> </candidate>
               |</entityProps>'''.stripMargin()

And a fragment String:

String frag = '<candidate> <id>5</id><key></key> </candidate>'

You can parse the document:

def xml = new XmlSlurper().parseText( doc )

And the fragment:

def fragxml = new XmlSlurper().parseText( frag )

Then, append the fragment to the root node of the document:

xml.appendNode( fragxml )

And stream this document back into a String:

String newDoc = new groovy.xml.StreamingMarkupBuilder().bind { mkp.yield xml }
println newDoc

That prints:

<entityProps>
  <candidate><id>1</id><key></key></candidate>
  <candidate><id>2</id><key></key></candidate>
  <candidate><id>3</id><key></key></candidate>
  <candidate><id>4</id><key></key></candidate>
  <candidate><id>5</id><key></key></candidate>
</entityProps>

(I added the newlines myself to make it easier to read... the actual String you get is all on one line)

tim_yates
  • 167,322
  • 27
  • 342
  • 338