title: Groovy: append an XML Node to an existing XML document
body: <p>I'm using Groovy and I'm trying to insert an xml node into a xml document parsed with XmlSlurper.
  I manage to add the node at the end of the document but not where I really need to.</p>
  
  <p>Original doc:</p>
  
  <pre><code>&lt;xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config"&gt; 
      &lt;ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false"&gt;
          &lt;ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config"&gt;
            &lt;con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/&gt;
            &lt;con:port&gt;
              &lt;con:name&gt;ChargeServicesPort&lt;/con:name&gt;
              &lt;con:namespace&gt;java:dk.tdc.apilink.logic.sessions.interfaces&lt;/con:namespace&gt;
            &lt;/con:port&gt;
            &lt;con:selector type="SOAP body"/&gt;
          &lt;/ser:binding&gt;
      &lt;/ser:coreEntry&gt;
  &lt;/xml-fragment&gt;
  </code></pre>
  
  <p>Fragment to add</p>
  
  <pre><code>def fragmentToAddXml = '''
  &lt;ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config"&gt;hello&lt;/ser:security&gt;
  '''
  </code></pre>
  
  <p>This is the code I'm using.</p>
  
  <pre><code>def root = new XmlSlurper().parseText(file.getText())
  
  root.'core-entry'.appendNode( fragmentToAddXml )
  def xmlBuilder = new groovy.xml.StreamingMarkupBuilder().bind{ mkp.yield root }
  </code></pre>
  
  <p>Please note that the new node should be placed <strong>before</strong> the "ser:binding" node.</p>
  
  <p>The result should be:</p>
  
  <pre><code>&lt;xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config"&gt; 
          &lt;ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false"&gt;
              &lt;ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config"&gt;hello&lt;/ser:security&gt;
  
              &lt;ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config"&gt;
                &lt;con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/&gt;
                &lt;con:port&gt;
                  &lt;con:name&gt;ChargeServicesPort&lt;/con:name&gt;
                  &lt;con:namespace&gt;java:dk.tdc.apilink.logic.sessions.interfaces&lt;/con:namespace&gt;
                &lt;/con:port&gt;
                &lt;con:selector type="SOAP body"/&gt;
              &lt;/ser:binding&gt;
          &lt;/ser:coreEntry&gt;
      &lt;/xml-fragment&gt;
  </code></pre>
  
  <p>Thanks</p>
  
  <p>Luciano</p>