Groovy: ajout d'un Nœud XML un document XML existant

Je suis en utilisant Groovy et je vais essayer d'insérer un nœud xml dans un document xml analysé avec XmlSlurper.
J'ai réussi à ajouter le nœud à la fin du document, mais pas là où j'en ai vraiment besoin.

Original doc:

<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>

Fragment d'ajouter

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>
'''

C'est le code que j'utilise.

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

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

Veuillez noter que le nouveau nœud doit être placé avant le "ser:" liaison de nœud.

Le résultat devrait être:

<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>

Grâce

Luciano

OriginalL'auteur Luciano Fiandesio | 2011-10-27