Recently, I received a large XML document that was rendered as a single line in a file, and so I wanted to write the formatted XML to a new file. Here's the Groovy script I wrote to do this:
import groovy.xml.*
if(args == null || args.length == 0) {
println "Please pass the name of the file as the first argument!"
return
}
String fileName = args[0]
String outFileName = fileName.replaceAll(".xml", "-pretty.xml");
def xml = new XmlSlurper().parse(new File("${fileName}"))
def file = new File("${outFileName}")
file.write(XmlUtil.serialize(xml))
Hope that helps someone!