meta programming XMLSlurper results

768 views Asked by At

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=&quot;http://twitter.com/Star_babey&quot;>@Star_babey</a>: But its only <b>twitter</b> tho star u wilding...lml</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

1

There are 1 answers

2
tim_yates On BEST ANSWER

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