元编程 XMLSlurper 结果

发布于 2024-10-14 10:16:54 字数 1393 浏览 7 评论 0原文

我已经吸收了一个 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=&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 中做的是显示像 这样的图像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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2024-10-21 10:16:54

我认为不需要元编程,您应该能够这样做:

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

那么这应该为您留下每个位置的字符串列表...

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文