我正在尝试构建一个 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)