meta programming XMLSlurper results
I've Slurped up a twitter feed where each entry looks like:
<entry>
<id>tag:search.twitter.com,2005:30481912300568576</id>
<published>2011-01-27T04:27:08Z</published>
<link type="text/html" rel="alternate" href="http://twitter.com/LadyCourtz/statuses/30481912300568576"/>
<title>U always right. ml</title>
<content type="html">U always right. T <a href="http://twitter.com/Star_babey">@Star_babey</a>: But its only <b>twitter</b> tho star u wilding...lml&开发者_Go百科lt;/content>
<updated>2011-01-27T04:27:08Z</updated>
<link type="image/png" rel="image" href="http://a2.twimg.com/profile_images/1221429153/248287865_normal.jpg"/>
etc etc
What I needed to do in grails/GSP was to display the image like <img src=${tweet.imgUrl}/>
So this looked like a good case for metaprogramming the XML result but I'm having trouble as a Groovy nooby.
See how there are at least 2 "link" nodes, the image url has a rel="image"
attribute. So I've tried...
def records = new XmlSlurper().parse(response.data)
records.entry.metaClass.imgUrl = { -> return delegate.link?.find{it?.@rel == 'image'}?.@href }
But errors like this I cannot get beyond:
groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChild.shout() is applicable for argument types: () values: []
Any help appreciated
No need for meta programming I don't think, you should just be able to do:
imageUrlList = new XmlSlurper().parse( response.data ).entry.link.findAll { it.@rel == 'image' }*.@href
Then that should leave you with a list of Strings for each location...
Are you passing the whole XmlSlurper back to the GSP? I'd probably just extract the data you need and send only that back
精彩评论