(在此附上对于列集(Marshalling)解释:对函数参数进行打包处理得过程,因为指针等数据,必须通过一定得转换,才能被另一组件所理解。可以说列集(Marshalling)是一种数据格式的转换方法。)
Grails同样支持自动列集(Marshalling) domain类 为XML,通过特定的转换器.
首先,导入grails.converters 类包到你的controller(控制器):
import grails.converters.*
现在,你可以使用下列高度易读的语法来自动转换domain类为XML:render Book.list() as XML
输出结果看上去像下面这样:<?xml version="1.0" encoding="iso-8859-1"?>
<list>
<book id="1">
<author>Stephen King</author>
<title>The Stand</title>
</book>
<book id="2">
<author>Stephen King</author>
<title>The Shining</title>
</book>
</list>
一个使用转换器的替代方法是使用Grails的codecs 特性. codecs特性提供了 encodeAsXML 和 encodeAsJSON方法:def xml = Book.list().encodeAsXML()
render xml
更多的XML 列集(Marshalling)信息见REST