Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlson/337353 to your computer and use it in GitHub Desktop.
Save xlson/337353 to your computer and use it in GitHub Desktop.
Example of updating xml with Groovy using XmlParser and NodeBuilder.
def exampleXmlString = '''<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<name>John</name>
<lastname>Doe</lastname>
<age>38</age>
</person>
</persons>'''
def exampleXml = new XmlParser().parseText(exampleXmlString)
def personBuilder = new NodeBuilder()
def personNode = personBuilder.person {
name('Jane')
lastname('Doe')
age('42')
}
assert exampleXml.append(personNode)
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(exampleXml)
println writer.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment