我刚刚尝试了 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>
,因为Feed验证程序会抛出此错误:
An item should not include both pubDate and dc:date
我该如何解决这个问题?我很想使用这个插件,但我需要一个有效的RSS。
答案 0 :(得分: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
在插件测试套件中测试了这个解决方法