J'ai besoin d'ajouter l'attribut @ à l'élément racine du fragment XML dans Groovy. Je veux utiliser XmlSlurper
. Comment faire? Ajouter des éléments est facile.Comment ajouter un attribut XML en utilisant Groovy?
4
A
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
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
- 1. Comment ajouter une ligne dans un xml en utilisant groovy
- 2. Comment puis-je ajouter un attribut XML en utilisant XSLT
- 3. ajouter un attribut en utilisant filecodemodel
- 4. Comment ajouter un attribut à une HashMap en utilisant JAXB?
- 5. Comment obtenir attribut xml en utilisant JAXB
- 6. Comment ajouter un attribut à un noeud XML sérialisé?
- 7. Ajouter un attribut XML Namespace à un fichier XML tiers?
- 8. Comment ajouter un attribut spécifique à chaque noeud dans un fichier Xml en utilisant Xsl?
- 9. Comment puis-je ajouter un attribut à un nœud XML en fonction d'un autre attribut?
- 10. Comment lier un attribut spécifique d'un XML en utilisant LINQ?
- 11. Créer un attribut en XML
- 12. Comment ajouter un attribut à un élément de schéma xml?
- 13. Comment ajouter un "#" dans cet attribut href en utilisant jQuery?
- 14. Comment ajouter un attribut à un élément XmlArray (XML Serialization)?
- 15. Comment ajouter un attribut à l'élément racine dans XML en utilisant XSLT?
- 16. Comment ajouter un attribut de valeur unique à chaque balise dans le xml en utilisant XSL
- 17. Comment ajouter à une réponse XML un attribut d'erreur en utilisant Ruby on Rails 3?
- 18. Comment ajouter un attribut XML dans un fichier XML en C#
- 19. Mappage xml-attribut en utilisant resteasy
- 20. Comment renommer un attribut en XML?
- 21. Ajouter un attribut à un nœud xml avec nant
- 22. comment ajouter des méthodes statiques en utilisant groovy mixin
- 23. Comment ajouter un attribut XML à l'aide de DataContract
- 24. Ajouter un attribut désactivé à l'élément d'entrée en utilisant Javascript
- 25. Comment définir dynamiquement un attribut de noeud en utilisant le modèle NodeBuilder dans groovy
- 26. JavaScript: Comment ajouter un attribut?
- 27. Comment interroger xsi: type d'un attribut utilisant Linq en XML?
- 28. Ajouter un attribut xmlns à un fichier XML.
- 29. Comment trouver un élément XML par attribut en utilisant LINQ to XML?
- 30. Ajouter un élément racine attribut
droit , c'était facile. Maintenant je vois que je faisais tout correctement :) J'ai seulement oublié de sérialiser la sortie. Merci! – Lukasz