元编程 XMLSlurper 结果
我已经吸收了一个 twitter feed,其中每个条目看起来像:
<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</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 中做的是显示像 这样的图像code> 所以这看起来像是对 XML 结果进行元编程的一个很好的例子,但我作为一个 Groovy 菜鸟遇到了麻烦。
看看至少有 2 个“链接”节点,图像 url 有一个 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: []
任何帮助表示赞赏
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</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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为不需要元编程,您应该能够这样做:
那么这应该为您留下每个位置的字符串列表...
您是否将整个 XmlSlurper 传递回 GSP?我可能只是提取您需要的数据并将其发回
No need for meta programming I don't think, you should just be able to do:
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