Groovy: Add an XML Node to an Existing XML Document

I am using Groovy and I am trying to insert an xml node into an XML document processed using XmlSlurper. I manage to add node to the end of the document, but not where I really need to.

Original document:

<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"> 
    <ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
        <ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
          <con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
          <con:port>
            <con:name>ChargeServicesPort</con:name>
            <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
          </con:port>
          <con:selector type="SOAP body"/>
        </ser:binding>
    </ser:coreEntry>
</xml-fragment>

Snippet to add

def fragmentToAddXml = '''
<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">hello</ser:security>
'''

This is the code I'm using.

def root = new XmlSlurper().parseText(file.getText())

root.'core-entry'.appendNode( fragmentToAddXml )
def xmlBuilder = new groovy.xml.StreamingMarkupBuilder().bind{ mkp.yield root }

Note that the new node must be placed before the "ser: binding" node.

The result should be:

<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"> 
        <ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
            <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">hello</ser:security>

            <ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
              <con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
              <con:port>
                <con:name>ChargeServicesPort</con:name>
                <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
              </con:port>
              <con:selector type="SOAP body"/>
            </ser:binding>
        </ser:coreEntry>
    </xml-fragment>

thank

Luciano

+4
source share
1 answer

Given xml (in line for testing)

def xml = '''<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"> 
    <ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
        <ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
          <con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
          <con:port>
            <con:name>ChargeServicesPort</con:name>
            <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
          </con:port>
          <con:selector type="SOAP body"/>
        </ser:binding>
    </ser:coreEntry>
</xml-fragment>'''

And the xml you want to add as:

def toadd = '''<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">
  hello
</ser:security>'''

Then you can analyze both of them (using XmlSlurperset to use namespaces via the 2nd parameter true)

def root = new XmlSlurper( false, true ).parseText( xml )
fragmentToAdd = new XmlSlurper( false, true ).parseText( toadd )

xml data node ( data, lastname)

root.coreEntry.appendNode( fragmentToAdd )

:

String outxml = groovy.xml.XmlUtil.serialize( root )
println outxml

:

<?xml version="1.0" encoding="UTF-8"?>
<xml-fragment>
  <ser:coreEntry xmlns:ser="http://www.bea.com/wli/sb/services" isTracingEnabled="false" isProxy="true" isEnabled="true">
    <ser:binding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" isSoap12="false" xsi:type="SOAP">
      <con:wsdl xmlns:con="http://www.bea.com/wli/sb/services/bindings/config" ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
      <con:port xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
        <con:name>ChargeServicesPort</con:name>
        <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
      </con:port>
      <con:selector xmlns:con="http://www.bea.com/wli/sb/services/bindings/config" type="SOAP body"/>
    </ser:binding>
    <ser:security>
    hello
  </ser:security>
  </ser:coreEntry>
</xml-fragment>

( 100%, , ); -)

, XmlParser :

def root = new XmlParser( false, true ).parseText( xml )
fragmentToAdd = new XmlParser( false, true ).parseText( toadd )

// Insert this new node at position 0 in the children of the first coreEntry node
root.find { it.name() == 'ser:coreEntry' }.children().add( 0, fragmentToAdd )

String outxml = groovy.xml.XmlUtil.serialize( root )
println outxml
+8

All Articles