2011-10-17 7 views

Répondre

11

Exécuter ce dans la console Groovy pour vérifier que cela fonctionne

import groovy.xml.StreamingMarkupBuilder 

// the original XML 
def input = "<foo><bar></bar></foo>" 

// add attributeName="attributeValue" to the root 
def root = new XmlSlurper().parseText(input) 
[email protected] = 'attributeValue' 

// get the modified XML and check that it worked 
def outputBuilder = new StreamingMarkupBuilder() 
String updatedXml = outputBuilder.bind{ mkp.yield root } 

assert "<foo attributeName='attributeValue'><bar></bar></foo>" == updatedXml 
+0

droit , c'était facile. Maintenant je vois que je faisais tout correctement :) J'ai seulement oublié de sérialiser la sortie. Merci! – Lukasz

1

ajoutant un attribut est le même que lire:

import groovy.xml.StreamingMarkupBuilder 

def input = ''' 
<thing> 
    <more> 
    </more> 
</thing>''' 

def root = new XmlSlurper().parseText(input) 

[email protected] = 'new' 

def outputBuilder = new StreamingMarkupBuilder() 
String result = outputBuilder.bind{ mkp.yield root } 

println result 

vous donnera:

<thing stuff='new'><more></more></thing> 
Questions connexes