簡體   English   中英

Groovy 的 MarkupBuilder 生成“id”XML 標記的異常

[英]Groovy's MarkupBuilder exception on generating "id" XML tag

我正在嘗試構建一個 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)

在我看來,您的 XML 構建器正試圖引用環境中的某些String變量。 由於 groovy builder 攔截丟失的方法調用,如果他們找到引用,他們將嘗試應用到它。 以下代碼可以重現您的錯誤:

def id = ""

new groovy.xml.MarkupBuilder().xml {
  id "90"
}

以下是好的:

def ids = ""

new groovy.xml.MarkupBuilder().xml {
  id "90"
}

重命名您的id變量應該可以解決問題


更新:

使用與作用域中的變量同名的標簽的另一種方法是使用(丑陋的)GString:

def id = ""

new groovy.xml.MarkupBuilder().xml {
  "${'id'}" "90"
}

遇到同樣的情況並與建造者進行資格鑒定,為我解決了這個問題。

def writer = new StringWriter()
    def builder = new MarkupBuilder(writer)
    builder.executions() {
        project.scalaVersions.each { scalaVersion ->
            def scalaMajorVer = '_' + scalaVersion.split('\\.')[0..1].join('.')
            def artifactIdStr = publication.artifactId.replaceAll(/_[0-9.]+$/, '') + scalaMajorVer

            execution() {
                builder.id(artifactIdStr) // qualify with builder to avoid collision
                phase('deploy')
                goals() { goal('deploy-file') }
                configuration() {
                    groupId(publication.groupId)
                    artifactId(artifactIdStr)
                    builder.version(project.version) // ditto.
                }
            }
        }
    }

我想對 Groovy 執行與 MarkupBuilder 相同的操作,但是在 Java 中,您是否知道有一個類或解決方案可以以相同的方式執行此操作? 謝謝 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM