0

我刚刚试用了grails feeds-plugin http://grails.org/Feeds+Plugin

它工作起来非常简单而且效果很好,但有一件事我不明白。

只要我添加一个publishedDate,比如:

     reviews.each() {
         review -> entry('fooTitle'){
             publishedDate = review.dtCreated
             review.rating + ' ' + review.comment
        }
     }

它生成两个标签:

      <pubDate>Tue, 04 Sep 2012 12:10:02 GMT</pubDate>
      <dc:date>2012-09-04T12:10:02Z</dc:date>

数据库中的条目是:

"dtCreated": ISODate("2013-01-15T00:52:47.0Z"),

但我只希望<pubDate>生成,因为提要验证器会引发此错误:

An item should not include both pubDate and dc:date 

我该如何解决这个问题?我很想使用这个插件,但我需要一个有效的 RSS。

4

1 回答 1

2

如果你不介意快速而肮脏的解决方案,你可以试试这个(它取自插件的集成测试并修改):

import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder
import feedsplugin.FeedBuilder
import com.sun.syndication.io.SyndFeedOutput

class TestController {
private renderToString(feedType, feedVersion, Closure closure) {
    def builder = new FeedBuilder()
    builder.feed(closure)

    def type = feedType
    def version = feedVersion

    SyndFeedOutput output = new SyndFeedOutput()
    def sw = new StringWriter()
    output.output(builder.makeFeed(type, version),sw)
    sw.toString()

}

private removeDcDate(String rssFeed) {

    def dom = new XmlSlurper().parseText(rssFeed)
    dom.channel[0].item.eachWithIndex { item, i ->
        dom.channel[0].item[i].date = {}
    }

    def newResp = XmlUtil.serialize(new StreamingMarkupBuilder().bind {
        mkp.declareNamespace (rdf:"http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        mkp.declareNamespace (dc:"http://purl.org/dc/elements/1.1/")
        mkp.declareNamespace (content:"http://purl.org/rss/1.0/modules/content/")
        mkp.declareNamespace (itunes:"http://www.itunes.com/dtds/podcast-1.0.dtd")
        mkp.declareNamespace (taxo:"http://purl.org/rss/1.0/modules/taxonomy/")

        mkp.yield dom 
    })

    newResp

}

def test = {
    def articles = ['A', 'B', 'C']

     def outStr= renderToString("rss", "2.0") {
        title = 'Test feed'
        link = 'http://somewhere.com/'
        description = "This is a test feed" 

        articles.each() { article ->
            entry("Title for \$article") {
                content(type:'text/html') {
                    return "Content for \$article"
                }

                link = 'http://somewhere.com/x'

                publishedDate = new Date()

            }
        }
    }
    def cleanedFeed = removeDcDate(outStr)

    render text: cleanedFeed, contentType: "application/rss+xml", encoding: "UTF-8"

    }
}

基本上它所做的是直接调用 feedbuilder 来获取 feed 的字符串表示,然后对其进行解析并删除“dc:date”标签。结果照常呈现。

我已经在带有 grails 1.3.7 的插件测试套件中测试了这个解决方法

于 2013-02-28T12:30:06.810 回答