Groovy xml parse then rebuild -


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

  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) Results = Author.Trusting ()  

This gives me the following output, but I love it with the well-formed closing tag Otherwise XPath query on new data then Will fail.

For example

  & lt; Student.name_name / & gt;  

Required to be created

  & lt; Student.name_name & gt; & Lt; /student.name_known>  

Any ideas ??

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

& lt; Student.name_name / & gt;

is perfectly well-formed, and XPath queries should work well on this XML structure.


Comments