Atom feed from Grails


/ Published in: Groovy
Save to your folder(s)

Here's a quick block of code that adds a valid Atom feed to you Grails application. Validate here: http://feedvalidator.org/check?url=http://aboutgroovy.com/item/atom


Copy this code and paste it in your HTML
  1. def atom = {
  2. params.max = 10
  3. params.sort = 'datePosted'
  4. params.order = 'desc'
  5. def tmpList = Item.list( params )
  6.  
  7. def feedHeader = """<feed xmlns="http://www.w3.org/2005/Atom">
  8. <title type="text">aboutGroovy.com</title>
  9. <link rel="alternate" type="text/html" href="http://aboutGroovy.com"/>
  10. <link rel="self" type="application/atom+xml" href="http://aboutGroovy.com/item/atom"/>
  11. <updated>2006-12-17T18:30:02Z</updated>
  12. <author><name>Scott Davis</name></author>
  13. <id>tag:aboutgroovy.com,2006-12-18:thisIsUnique</id>
  14. <generator uri="http://aboutGroovy.com" version="0.0.1">Hand-rolled Grails code</generator>
  15. """
  16.  
  17. def feedFooter = "</feed>"
  18.  
  19. def df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'-07:00'")
  20. tmpList.each{item ->
  21. def x = new groovy.xml.MarkupBuilder(sw)
  22. x.entry(xmlns:"http://www.w3.org/2005/Atom"){
  23. author{name("Scott Davis")}
  24. published(df.format(item.datePosted))
  25. updated(df.format(item.datePosted))
  26. link(href:"http://aboutGroovy.com/item/show/${item.id}",
  27. rel:"alternate", title:item.title, type:"text/html")
  28. id("tag:aboutgroovy.com,2006:/item/show/${item.id}")
  29. title(type:"text", item.title)
  30. content(type:"xhtml"){
  31. div(xmlns:"http://www.w3.org/1999/xhtml"){
  32. p("Category: ${item.type}")
  33. p{a(href:item.url, "Original Source")}
  34. p(item.shortDescription)
  35. p(item.description)
  36. }
  37. }
  38. }
  39. feed.append(sw.toString() + "\n")
  40. }
  41.  
  42. response.setContentType("application/atom+xml")
  43. render "${feedHeader}${feed}${feedFooter}"
  44. }

URL: http://aboutgroovy.com/item/atom

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.