元编程XMLSlurper结果

时间:2011-01-28 05:29:06

标签: grails groovy metaclass xmlslurper

我已经推出了一个推文,其中每个条目都是:

<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"/>

等等

我需要在grails / GSP中做的就是显示像<img src=${tweet.imgUrl}/>这样的图像所以这看起来像是对XML结果进行元编程的好例子,但我在Groovy nooby时遇到了麻烦。

查看至少有2个“链接”节点,图片网址具有rel="image"属性。所以我试过......

def records = new XmlSlurper().parse(response.data)
records.entry.metaClass.imgUrl = { -> return delegate.link?.find{it?.@rel == 'image'}?.@href }

但是这样的错误我无法超越:

groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChild.shout() is applicable for argument types: () values: []

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

不需要元编程我不认为,你应该能够做到:

imageUrlList = new XmlSlurper().parse( response.data ).entry.link.findAll { it.@rel == 'image' }*.@href

那么你应该为每个位置留下一个字符串列表......

您是否将整个XmlSlurper传递回GSP?我可能只是提取您需要的数据并仅发送回来