如何使用Grails REST webservices输出xml命名空间

时间:2010-08-14 20:12:46

标签: xml rest grails namespaces

它是一个Grails应用程序,我们希望能够将XML命名空间添加到为REST客户端生成的xml中。大多数xml使用深度转换器使用“render foo as XML”输出。

所以输出必须是这样的: <foo xmlns:myns='http://mycompany.com/myproduct/ver'> ... </foo>

1 个答案:

答案 0 :(得分:1)

http://groovy.codehaus.org/Creating+XML+using+Groovy%27s+MarkupBuilder

def xml = new MarkupBuilder(writer)
xml.'rec:records'('xmlns:rec': 'http://groovy.codehaus.org') {
  car(name:'HSV Maloo', make:'Holden', year:2006) {
    country('Australia')
    record(type:'speed', ' Truck with speed of 271kph')
  }
}

result

<rec:records xmlns:rec='http://groovy.codehaus.org'>
  <car name='HSV Maloo' make='Holden' year='2006'>
    <country>Australia</country>
    <record type='speed'> Truck with speed of 271kph</record>
  </car>
</rec:records>