Groovy xml edit value causes duplicate -


With more than a little help from daviderossi.blogspot.com, I used some code to change an xml value Is another

This gives me the following output which edits the value on both 'ix' status, but eventually adds another copy. If I search with LastIndexOf and delete it, then it removes the first event. Any idea why the code may be doing this, or how to reduce this unwanted effects?

  def fm_xml = '' '& lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Malong & gt; & Lt; Enquiry.ID & gt; SC11147 & LT; /Enquiry.ID> & Lt; Student.name_middle & gt; & Lt; /student.name_middle> & Lt; Student.name_name & gt; & Lt; /student.name_known> & Lt; Student.name_previous & gt; & Lt; /student.name_previous> & Lt; Student.name_cert & gt; John Renfrew & lt; /student.name_cert> & Lt; Student.detail_gender & gt; M & LT; /student.detail_gender> & Lt; Student.sign_name & gt; John Renfrew & lt; /student.sign_name> & Lt; Student.sign_date & gt; 05/01/2010 and LT; /student.sign_date> & Lt; / MAlong & gt; '' '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} Author = New StringWriter () New XmlNodePrinter (new PrintWriter (author)). Print (xml) result = author.trusting ()  

RESULT

  & lt; Melong & gt; & Lt; Enquiry.ID & gt; SC11147 & lt; / Inquiry.ID & gt; & Lt; Student.name_middle & gt; New & lt; /student.name_middle> & Lt; Student.name_name / & gt; & Lt; Student.name_previous /> & Lt; Student.name_cert & gt; John Renfrew & lt; /student.name_cert> & Lt; Student.detail_gender & gt; M & LT; /student.detail_gender> & Lt; Student.sign_name & gt; John Renfrew & lt; /student.sign_name> & Lt; Student.sign_date & gt; 05/01/2010 & lt; /student.sign_date> & Lt; Student.name_middle & gt; New & lt; /student.name_middle> & Lt; / Malong & gt; Using Groovy Class to work with your XML makes your code easier and improves readability. I have created a sample script on Groovy Console where you can evaluate it:  
  • (first version)
  • (with very print)

    example-code import groovy.xml. Streaming markupbilder import groovy.xml.xmlUtil def prettyprint (xml) {XmlUtil.serialize (new Streaming markupbilder (). Bind {Mkp.yield xml})} def input = '' '& lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Melong & gt; & Lt; Enquiry.ID & gt; SC11147 & LT; /Enquiry.ID> & Lt; Student.name_middle & gt; & Lt; /student.name_middle> & Lt; Student.name_name & gt; & Lt; /student.name_known> & Lt; Student.name_previous & gt; & Lt; /student.name_previous> & Lt; Student.name_cert & gt; John Renfrew & lt; /student.name_cert> & Lt; Student.detail_gender & gt; M & LT; /student.detail_gender> & Lt; Student.sign_name & gt; John Renfrew & lt; /student.sign_name> & Lt; Student.sign_date & gt; 05/01/2010 and LT; /student.sign_date> & Lt; / MAlong & gt; '' 'Def root = new XML slipper (). ParseText (input) println "input \ n" + printprint (root) // static route root root. Stentant Name_mild '=' middle name '// Variable the way the root.setProperty ("student.name_previous", "PREVIOUS NAME") println "Output \ n" + Print Print (root)

Reference:


Comments