String indentXml(xml) {
def factory = TransformerFactory.newInstance()
factory.setAttribute("indent-number", 2);
Transformer transformer = factory.newTransformer()
transformer.setOutputProperty(OutputKeys.INDENT, 'yes')
StreamResult result = new StreamResult(new StringWriter())
transformer.transform(new StreamSource(new ByteArrayInputStream(xml.toString().bytes)), result)
return result.writer.toString()
}
xml is the output from the .bind call on StreamingMarkupBuilder. And the setAttribute call on TransformerFactory is needed to get around a JDK bug.
2 comments:
Thanks. You saved me some time :-)
Hi, you can also use XmlUtil.serialize(xml)
Post a Comment