Created
March 19, 2010 08:41
-
-
Save xlson/337353 to your computer and use it in GitHub Desktop.
Example of updating xml with Groovy using XmlParser and NodeBuilder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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