在grails动作中给出了这个:
def xml = {
rss(version: '2.0') {
...
}
}
render(contentType: 'application/rss+xml', xml)
我明白这一点:
<rss><channel><title></title><description></description><link></link><item></item></channel></rss>
有一种简单的方法来打印XML吗?也许在渲染方法中内置了一些东西?
答案 0 :(得分:19)
这是一种使用Groovy代码完美打印XML的简单方法:
def xml = "<rss><channel><title></title><description>" +
"</description><link></link><item></item></channel></rss>"
def stringWriter = new StringWriter()
def node = new XmlParser().parseText(xml);
new XmlNodePrinter(new PrintWriter(stringWriter)).print(node)
println stringWriter.toString()
结果:
<rss>
<channel>
<title/>
<description/>
<link/>
<item/>
</channel>
</rss>
答案 1 :(得分:4)
根据reference docs,您可以使用以下配置选项启用漂亮打印:
grails.converters.default.pretty.print (Boolean)
//Whether the default output of the Converters is pretty-printed ( default: false )
答案 2 :(得分:3)
使用MarkupBuilder来打印您的Groovy xml
def writer = new StringWriter()
def xml = new MarkupBuilder (writer)
xml.rss(version: '2.0') {
...
}
}
render(contentType: 'application/rss+xml', writer.toString())
答案 3 :(得分:2)
使用XmlUtil:
def xml = "<rss><channel><title></title><description>" +
"</description><link></link><item></item></channel></rss>"
println XmlUtil.serialize(xml)