Grails Cookbook - A collection of tutorials and examples

Groovy Edit/Modify XML Examples

Sometimes, it is not enough to parse or create XML documents. There are times when we need to edit an XML information for further processing. Below are examples on how to modify or edit existing XML document.

Simple Example of Editing Existing XML

Here is a simple example on how to parse an existing XML document stored in a String, modify the information, and store the modified XML to a new String:
package test
import groovy.xml.MarkupBuilder
/**
 * A Simple Example that Modifies XML information.
 */
class Test {
    static stringXML = '<person>'+
        '<firstName>John</firstName><lastName>Doe</lastName><age>25</age>'+
        '</person>'
    static main(args) {
        def person = new XmlParser().parseText(stringXML)
        person.age[0].value = 26
        def stringWriter = new StringWriter()
        new XmlNodePrinter(new PrintWriter(stringWriter)).print(person)
        def newXml = stringWriter.toString()
        println newXml
    }
}
The example uses XmlNodePrinter to output the XML representation of an object. Note that we use XmlParser to parse the original XML. This is because XmlParser and XmlNodePrinter works well together. The output of the code is shown below:
<person>
  <firstName>
    John
  </firstName>
  <lastName>
    Doe
  </lastName>
  <age>
    26
  </age>
</person>
As expected, the age was updated from 25 to 26.

Modify Multiple Portion of an XML

We can also modify multiple places in an XML document. Below is an example that modifies the first name of all person whose age is greater than 20. The fist name is appended with the word "Old":
package test
import groovy.xml.MarkupBuilder
/**
 * A simple example of modifying multiple portion of an XML.
 */
class Test {
    static stringXML = 
        '<personDatabase>'+
            '  <person><firstName>John</firstName><lastName>Doe</lastName><age>25</age></person>'+
            '  <person><firstName>Jane</firstName><lastName>Smith</lastName><age>31</age></person>'+
            '  <person><firstName>Robert</firstName><lastName>Doe</lastName><age>11</age></person>'+
            '  <person><firstName>Michael</firstName><lastName>Smith</lastName><age>55</age></person>'+
            '  <person><firstName>Scott</firstName><lastName>Williams</lastName><age>35</age></person>'+
            '  <person><firstName>Alice</firstName><lastName>Anthony</lastName><age>14</age></person>'+
        '</personDatabase>'
    static main(args) {
        def people = new XmlParser().parseText(stringXML)
        people.person.findAll { p ->
            p.age[0].text().toInteger() > 20
        }.each { p ->
            p.firstName[0].value = "Old " + p.firstName[0].text()
        }
        def stringWriter = new StringWriter()
        new XmlNodePrinter(new PrintWriter(stringWriter)).print(people)
        def newXml = stringWriter.toString()
        println newXml
    }
}
As shown above, we use findAll to filter all entries that represent a person with age greater than 20. Then iterate over the result to modify the first name. The expected output of the code is:
<personDatabase>
  <person>
    <firstName>
      Old John
    </firstName>
    <lastName>
      Doe
    </lastName>
    <age>
      25
    </age>
  </person>
  <person>
    <firstName>
      Old Jane
    </firstName>
    <lastName>
      Smith
    </lastName>
    <age>
      31
    </age>
  </person>
  <person>
    <firstName>
      Robert
    </firstName>
    <lastName>
      Doe
    </lastName>
    <age>
      11
    </age>
  </person>
  <person>
    <firstName>
      Old Michael
    </firstName>
    <lastName>
      Smith
    </lastName>
    <age>
      55
    </age>
  </person>
  <person>
    <firstName>
      Old Scott
    </firstName>
    <lastName>
      Williams
    </lastName>
    <age>
      35
    </age>
  </person>
  <person>
    <firstName>
      Alice
    </firstName>
    <lastName>
      Anthony
    </lastName>
    <age>
      14
    </age>
  </person>
</personDatabase>
As seen, only people with age greater than 20 were altered.
Posts about XML