Best way to pretty print XML response in grails

后端 未结 4 2073
耶瑟儿~
耶瑟儿~ 2020-12-15 10:17

Given this in a grails action:

def xml = {
    rss(version: \'2.0\') {
        ...
    }
}
render(contentType: \'application/rss+xml\', xml)
<
相关标签:
4条回答
  • 2020-12-15 10:26

    According to the reference docs, you can use the following configuration option to enable pretty printing:

     grails.converters.default.pretty.print (Boolean)
     //Whether the default output of the Converters is pretty-printed ( default: false )
    
    0 讨论(0)
  • 2020-12-15 10:28

    Use MarkupBuilder to pretty-print your Groovy xml

    def writer = new StringWriter()
    def xml = new MarkupBuilder (writer)
    
    xml.rss(version: '2.0') {
            ...
        }
    }
    
    render(contentType: 'application/rss+xml', writer.toString())
    
    0 讨论(0)
  • 2020-12-15 10:40

    Use XmlUtil :

    def xml = "<rss><channel><title></title><description>" +
       "</description><link></link><item></item></channel></rss>"
    
    println XmlUtil.serialize(xml)
    
    0 讨论(0)
  • 2020-12-15 10:41

    This is a simple way to pretty-print XML, using Groovy code only:

    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()
    

    results in:

    <rss>
      <channel>
        <title/>
        <description/>
        <link/>
        <item/>
      </channel>
    </rss>
    
    0 讨论(0)
提交回复
热议问题