Quantcast

generating and returning xml

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

generating and returning xml

Jeff Brown-2
I want to create a link in one of my views that will return dynamically
generated xml to the browser.  In my view I have something like this...

<a href="${createLink(action:'xmlmovies')}">get xml</a>

In my controller I want xmlmovies to build up the xml and return it to
the browser.  I think I may want to do something like this...

def xmlmovies = {
  def domBuilder = groovy.xml.DOMBuilder.newInstance()
  def movies = domBuilder.movies() {
    Movie.findAll().each {
      movie(title:it.title)
    }
  }
}

At that point, I am not sure how to render the xml back to the
response.  I expect I may need to call the render() method and set the
contentType attribute to 'text/xml' but I am not sure exactly.

Any help would be appreciated.

Thanks.



jb

--
Jeff Brown
[hidden email]
Principal Software Engineer
Object Computing Inc.
http://www.ociweb.com/

Autism Strikes 1 in 250
Find The Cause ~ Find The Cure
http://www.jeffandbetsy.net/walkfar2005/
http://www.naar.org/ 



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: generating and returning xml

a.shneyderman
render method of the controller, maybe
http://www.grails.org/Controller+Dynamic+Methods#ControllerDynamicMethods-render

On 8/3/06, Jeff Brown <[hidden email]> wrote:

> I want to create a link in one of my views that will return dynamically
> generated xml to the browser.  In my view I have something like this...
>
> <a href="${createLink(action:'xmlmovies')}">get xml</a>
>
> In my controller I want xmlmovies to build up the xml and return it to
> the browser.  I think I may want to do something like this...
>
> def xmlmovies = {
>   def domBuilder = groovy.xml.DOMBuilder.newInstance()
>   def movies = domBuilder.movies() {
>     Movie.findAll().each {
>       movie(title:it.title)
>     }
>   }
> }
>
> At that point, I am not sure how to render the xml back to the
> response.  I expect I may need to call the render() method and set the
> contentType attribute to 'text/xml' but I am not sure exactly.
>
> Any help would be appreciated.
>
> Thanks.
>
>
>
> jb
>
> --
> Jeff Brown
> [hidden email]
> Principal Software Engineer
> Object Computing Inc.
> http://www.ociweb.com/
>
> Autism Strikes 1 in 250
> Find The Cause ~ Find The Cure
> http://www.jeffandbetsy.net/walkfar2005/
> http://www.naar.org/
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Thanks,
Alex.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: generating and returning xml

graemer
In reply to this post by Jeff Brown-2
render(contentType:'text/xml') {
    movies  {
       for( m in Movie.findAll() ) {
             movie(title:m.title)
         }
    }
}

The above will do it

Cheers
Graeme


On 8/3/06, Jeff Brown <[hidden email]> wrote:

> I want to create a link in one of my views that will return dynamically
> generated xml to the browser.  In my view I have something like this...
>
> <a href="${createLink(action:'xmlmovies')}">get xml</a>
>
> In my controller I want xmlmovies to build up the xml and return it to
> the browser.  I think I may want to do something like this...
>
> def xmlmovies = {
>   def domBuilder = groovy.xml.DOMBuilder.newInstance()
>   def movies = domBuilder.movies() {
>     Movie.findAll().each {
>       movie(title:it.title)
>     }
>   }
> }
>
> At that point, I am not sure how to render the xml back to the
> response.  I expect I may need to call the render() method and set the
> contentType attribute to 'text/xml' but I am not sure exactly.
>
> Any help would be appreciated.
>
> Thanks.
>
>
>
> jb
>
> --
> Jeff Brown
> [hidden email]
> Principal Software Engineer
> Object Computing Inc.
> http://www.ociweb.com/
>
> Autism Strikes 1 in 250
> Find The Cause ~ Find The Cure
> http://www.jeffandbetsy.net/walkfar2005/
> http://www.naar.org/
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: generating and returning xml

maxarbos
How would I do this if each movie was it's own object and there was a title, year, genre, etc..?

so it would be :

<movies>
    <movie>
        <title>Heat</title>
        <genre>Action</genre>
        <year>1999</year>
    </movie>
    <movie>
        <title>Jaws</title>
        <genre>Suspense</genre>
        <year>1973</year>
    </movie>
</movies>

thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: generating and returning xml

wdbetts
1) Use Automatic XML Marshalling
<a href="http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.7 XML and JSON Responses">http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.7 XML and JSON Responses
def movieList = [movie1, movie2]
movieList as XML
<list>
 <movie>
...
</movie>
<movie>
</movie>
</list>

2) User contentType on controller render
If you really need the root node to have the name <movies>, then you will need to build your own.  If you are returning this from a controller you can use the content type of the render method.

render contentType: "application/xml", {
      movies {
        movieList.each {movieIt ->
          movie {
            title movieIt.title
            genre movieIt.genre
            year movieIt.year
          }
        }
      }
    }

3) Use Groovy's MarkupBuilder
If none of the above work for you:

On Sun, Sep 19, 2010 at 7:55 PM, maxarbos <[hidden email]> wrote:

How would I do this if each movie was it's own object and there was a title,
year, genre, etc..?

so it would be :

<movies>
   <movie>
       <title>Heat</title>
       <genre>Action</genre>
       <year>1999</year>
   </movie>
   <movie>
       <title>Jaws</title>
       <genre>Suspense</genre>
       <year>1973</year>
   </movie>
</movies>

thanks.
--
View this message in context: http://grails.1312388.n4.nabble.com/generating-and-returning-xml-tp1315259p2546446.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: generating and returning xml

maxarbos
thank you very much.
I was missing using the node 'movies' as a closure.

Thanks again!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: generating and returning xml

Artur Ejsmont
WOW ALE JESTES MASTER!

mysle ze tytulem:

zaproszenia slubne
Ejsmont Artur f 981cg


Posralem sie z radosci : ) super ze tak szybko to bede mial juz
odchaczone! dzieki braciszku

napisz mailiczka jak odsapniesz : )

art

On 20 September 2010 14:42, maxarbos <[hidden email]> wrote:

>
> thank you very much.
> I was missing using the node 'movies' as a closure.
>
> Thanks again!
> --
> View this message in context: http://grails.1312388.n4.nabble.com/generating-and-returning-xml-tp1315259p2547000.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...