Melhor maneira de resposta XML de impressão bonita em grails
-
04-07-2019 - |
Pergunta
Dada esta em uma ação grails:
def xml = {
rss(version: '2.0') {
...
}
}
render(contentType: 'application/rss+xml', xml)
Eu vejo isso:
<rss><channel><title></title><description></description><link></link><item></item></channel></rss>
Existe uma maneira fácil de imprimir muito o XML? Algo embutido no método render, talvez?
Solução
De acordo com a documentos de referência , você pode usar a seguinte opção de configuração para ativar a impressão bastante:
grails.converters.default.pretty.print (Boolean)
//Whether the default output of the Converters is pretty-printed ( default: false )
Outras dicas
Este é um simples maneira de XML bem-impressão, utilizando o código Groovy apenas:
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()
resulta em:
<rss>
<channel>
<title/>
<description/>
<link/>
<item/>
</channel>
</rss>
Use MarkupBuilder para pretty-imprimir o Groovy xml
def writer = new StringWriter()
def xml = new MarkupBuilder (writer)
xml.rss(version: '2.0') {
...
}
}
render(contentType: 'application/rss+xml', writer.toString())
Use XmlUtil:
def xml = "<rss><channel><title></title><description>" +
"</description><link></link><item></item></channel></rss>"
println XmlUtil.serialize(xml)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow