views:

595

answers:

2

Let's say I have a class called Store that has many Employees. My RESTful listXML method looks like this:

def listXML = {
    render Store.list() as XML
}

And the result looks like this:

<stores>
  <store id="1">
   <name>My Store</name>
   <employees>
     <employee id="1" />
   </employees>
  </store>
</store>

My question is, how do I include all the data of each Employee class, so that my XML looks something like this?

   <stores>
      <store id="1">
       <name>My Store</name>
       <employees>
         <employee id="1">
           <name>John Smith</name>
           <hireDate>2008-01-01</hireDate>
         </employee>
       </employees>
      </store>
    </store>
+3  A: 

In your controller, you'll want to import the deep converter:

import grails.converters.deep.XML

You can read about it in the first couple of paragraphs of the Converters Reference.

Rob Hruska
Wow- thanks so much for the quickdraw answer. I'm so glad the solution was so easy!
Mike Sickler
Heh, no problem. Glad I could help.
Rob Hruska
+4  A: 

As of Grails 1.1 you will be able to configure Grails to default to deeply serializing by including this in your grails-app/conf/Config.groovy:

grails.converters.xml.default.deep = true

1.1 also introduces named configurations for Converters. The deep converters will be deprecated and the named configuration "deep" should be used instead.

XML.use("deep") {
   render model as XML
}
Siegfried Puchbauer
Unfortunately, this only allows for static configuration, which sometimes isn't optimal if you only sometimes need to render deep XML (and other times render shallow). There's a JIRA open on this (http://jira.codehaus.org/browse/GRAILS-5972), so after it's fixed, the solution for this issue may change again :).
Rob Hruska