【发布时间】:2012-12-26 09:51:51
【问题描述】:
我正在尝试构建 XML 提要,而 Groovy 的 MarkupBuilder 让我很头疼:
def newsstandFeed(def id) {
def publication = Publication.get(id)
def issues = issueService.getActiveIssuesForPublication(publication)
def updateDate = DateUtil.getRFC3339DateString(publication.lastIssueUpdate)
def writer = new StringWriter()
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
def xml = new MarkupBuilder(writer)
xml.feed('xmlns':"http://www.w3.org/2005/Atom", 'xmlns:news':"http://itunes.apple.com/2011/Newsstand") {
updated("${updateDate}")
issues.each { issue ->
entry {
id (issue.id)
updated("${DateUtil.getRFC3339DateString(issue.lastUpdated)}")
published("${DateUtil.getRFC3339DateString(issue.releaseDate)}")
summary(issue.summary)
"news:cover_art_icons" {
"news:cover_art_icon" (size:"SOURCE", src:"${issue.cover.remotePath}")
}
}
}
}
return writer.toString()
}
我得到了这个例外:
Class groovy.lang.MissingMethodException
No signature of method: java.lang.String.call() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [CYB_001] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure)
“CYB_001”是第一个“id”属性。
如果我将“id”重命名为“ids”或其他任何名称,它会起作用,并返回正确的 XML 文档:
....
issues.each { issue ->
entry {
ids ("${issue.id}")
...
任何想法为什么会发生这种情况,以及如何解决这个问题?
环境是 Grails 2.1.1(我假设是 Groovy 1.8)
【问题讨论】:
-
删除引号不起作用,顺便说一句:id (issue.id) - 相同的异常
-
你为什么不直接使用 feed 插件呢? grails.org/Feeds+Plugin
-
这段代码之前是什么?你能发布整个方法块吗?
-
好点。事实上,我已经将它用于一些 RSS 提要(相同的内容,只是不同的格式),但我很难让它输出符合 Apple 规范 itunesconnect.apple.com/docs/NewsstandAtomFeedSpecification.pdf 的原子提要
-
Will P - 谢谢!实际上,我有一个局部变量 id - 如果我重命名它,它可以工作,呵呵。 ;-)(将您的评论放在答案中,我会接受)。
标签: java xml grails groovy markupbuilder