2010-07-19 4 views
1

Avec plus d'un peu d'aide de daviderossi.blogspot.com j'ai réussi à obtenir un code de travail pour remplacer une valeur xml avec un autre

Cela me donne le résultat suivant toutes deux modifications de la valeur à la position «ix» MAIS ajoute également un deuxième exemplaire à la fin. Si je le recherche avec LastIndexOf et le supprime, il supprime la première occurrence. Des idées sur pourquoi le code pourrait faire cela, ou comment atténuer cet effet indésirable?

Groovy causes valeur de modification xml en double

def fm_xml = '''<?xml version="1.0" encoding="UTF-8"?> 
<MAlong> 
<Enquiry.ID>SC11147</Enquiry.ID> 
<student.name_middle></student.name_middle> 
<student.name_known></student.name_known> 
<student.name_previous></student.name_previous> 
<student.name_cert>John REnfrew</student.name_cert> 
<student.detail_gender>M</student.detail_gender> 
<student.sign_name>John Renfrew</student.sign_name> 
<student.sign_date>05/01/2010</student.sign_date> 
</MAlong>''' 

xml = new XmlParser().parseText(fm_xml) 
ix = xml.children().findIndexOf{it.name() =='student.name_middle'} 
nn = new Node(xml, 'student.name_middle', "NEW") 
if (ix != -1) { 
xml.children()[ix] = nn 
nn.parent = xml 
} 
writer = new StringWriter() 
new XmlNodePrinter(new PrintWriter(writer)).print(xml) 
result = writer.toString() 

RÉSULTAT

<MAlong> 
<Enquiry.ID> 
SC11147 
</Enquiry.ID> 
<student.name_middle> 
NEW 
</student.name_middle> 
<student.name_known/> 
<student.name_previous/> 
<student.name_cert> 
John REnfrew 
</student.name_cert> 
<student.detail_gender> 
M 
</student.detail_gender> 
<student.sign_name> 
John Renfrew 
</student.sign_name> 
<student.sign_date> 
05/01/2010 
</student.sign_date> 
<student.name_middle> 
NEW 
</student.name_middle> 
</MAlong> 

Répondre

1

Utilisation de la classe Groovy XMLSlurper pour travailler avec votre XML rend votre code plus facile et améliore la lisibilité. J'ai créé un exemple de script à la console Groovy où vous pouvez évaluer ceci:

Exemple-Code

import groovy.xml.StreamingMarkupBuilder 
import groovy.xml.XmlUtil 

def prettyprint(xml) { 
    XmlUtil.serialize(new StreamingMarkupBuilder().bind { mkp.yield xml }) 
} 

def input = '''<?xml version="1.0" encoding="UTF-8"?><MAlong> 
<Enquiry.ID>SC11147</Enquiry.ID> 
<student.name_middle></student.name_middle> 
<student.name_known></student.name_known> 
<student.name_previous></student.name_previous> 
<student.name_cert>John REnfrew</student.name_cert> 
<student.detail_gender>M</student.detail_gender> 
<student.sign_name>John Renfrew</student.sign_name> 
<student.sign_date>05/01/2010</student.sign_date> 
</MAlong>''' 

def root = new XmlSlurper().parseText(input) 
println "Input\n" + prettyprint(root) 

// static way 
root.'student.name_middle' = "MIDDLE NAME" 

// variable way 
root.setProperty("student.name_previous", "PREVIOUS NAME") 

println "Output\n" + prettyprint(root​)​ 

Référence:

+0

plus propre, beaucoup plus facile à lire, mais il jette une erreur: [student.name_middle] est une expression constante, mais il doit être une expression variable Je voudrais explorer cela car il semble très efficace.
Correction des valeurs répétées en faisant nn = new Node (null, 'student.name_middle', "NEW") –

+0

Le script ci-dessus devrait fonctionner tel quel. J'ai ajouté une deuxième déclaration de modification montrant la modification des champs avec des noms dynamiques. – stefanglase

+0

La sortie de cela produit une seule ligne. Y at-il un moyen de recréer l'arbre avec Markupbuilder ?? –