title: Copy a node from one XML tree to another XML tree in Groovy
body: <p>In SoapUI I want to use a Groovy script for picking a xml file from a directory, find a specific node tree in it and insert into a test request.</p>
  
  <p>Let's consider the following file in my directory <code>res/001/data.xml</code> with the content</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.org"&gt;
     &lt;soapenv:Header/&gt;
     &lt;soapenv:Body&gt;
        &lt;ns:list&gt;
           &lt;ns:item attr1="yes"&gt;
               &lt;title&gt;Apple&lt;/title&gt;
               &lt;quant&gt;3&lt;/quant&gt;
           &lt;/ns:item&gt;
           &lt;ns:item attr1="no"&gt;
               &lt;title&gt;Banana&lt;/title&gt;
               &lt;quant&gt;0&lt;/quant&gt;
           &lt;/ns:item&gt;
        &lt;/ns:list&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>And the soap request template:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com"&gt;
     &lt;soapenv:Header/&gt;
     &lt;soapenv:Body&gt;
        &lt;ns:mylist/&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>I try to read the list into my request using XmlHolder in the script:</p>
  
  <pre><code>def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
  def xml
  def req = groovyUtils.getXmlHolder( "SendData#Request" )
  req.namespaces["ns1"] = "http://examle.com"
  
  def folder = new File('res')
  
  if (folder.exists())
  {
      folder.eachDir( ) 
      { f -&gt;
          def xmlfile = new File( f.absolutePath + "/data.xml")
          if( xmlfile.exists() )
          {
              def source = new com.eviware.soapui.support.XmlHolder( xmlfile.text )
              source.namespaces["ns2"] = "http://example.org"
              use (groovy.xml.dom.DOMCategory)
              {
                  for( item in source.getDomNodes( "//ns2:list/ns2:item" )) 
                  {
                      log.info item.xml
                      req["//ns1:mylist[1]"].appendChild(item.xml)
                  }
              }
          }
      }
  }
  
  req.updateProperty()
  </code></pre>
  
  <p>My expected result would be:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com"&gt;
     &lt;soapenv:Header/&gt;
     &lt;soapenv:Body&gt;
        &lt;ns:mylist&gt;
           &lt;ns:item attr1="yes"&gt;
               &lt;title&gt;Apple&lt;/title&gt;
               &lt;quant&gt;3&lt;/quant&gt;
           &lt;/ns:item&gt;
           &lt;ns:item attr1="no"&gt;
               &lt;title&gt;Banana&lt;/title&gt;
               &lt;quant&gt;0&lt;/quant&gt;
           &lt;/ns:item&gt;
        &lt;/ns:mylist&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p><strong>Question:</strong> How can I achieve this?</p>
  
  <p>My current solution gives an error because <code>req["//ns1:mylist[1]"]</code> is null. I have tried many different methods now bot none of them worked.</p>