La mejor manera de imprimir la respuesta XML en gráficos.
-
04-07-2019 - |
Pregunta
Dado esto en una acción de griales:
def xml = {
rss(version: '2.0') {
...
}
}
render(contentType: 'application/rss+xml', xml)
Veo esto:
<rss><channel><title></title><description></description><link></link><item></item></channel></rss>
¿Hay una manera fácil de imprimir bonito el XML? ¿Algo integrado en el método de render, quizás?
Solución
De acuerdo con los documentos de referencia , puede usar la siguiente opción de configuración para permitir una impresión bonita:
grails.converters.default.pretty.print (Boolean)
//Whether the default output of the Converters is pretty-printed ( default: false )
Otros consejos
Esta es una manera simple de imprimir bastante XML, usando solo el código Groovy:
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 en:
<rss>
<channel>
<title/>
<description/>
<link/>
<item/>
</channel>
</rss>
Usa MarkupBuilder para imprimir bastante tu Groovy xml
def writer = new StringWriter()
def xml = new MarkupBuilder (writer)
xml.rss(version: '2.0') {
...
}
}
render(contentType: 'application/rss+xml', writer.toString())
Usar XmlUtil:
def xml = "<rss><channel><title></title><description>" +
"</description><link></link><item></item></channel></rss>"
println XmlUtil.serialize(xml)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow