简体   繁体   English

Groovy 的 MarkupBuilder 生成“id”XML 标记的异常

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

I'm trying to construct a XML feed, and Groovy's MarkupBuilder is giving me headaches:我正在尝试构建一个 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()
}

I get this exception:我得到这个例外:

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" is the first "id" attribute. “CYB_001”是第一个“id”属性。

If I rename "id" it to "ids" or anything else, it works, and returns a proper XML document:如果我将“id”重命名为“ids”或其他任何名称,它就会工作,并返回一个正确的 XML 文档:

            ....
            issues.each { issue ->
            entry {
                ids ("${issue.id}")
                ...

Any ideas why this is happening, and how I can work around the problem?任何想法为什么会发生这种情况,以及我如何解决这个问题?

The environment is Grails 2.1.1 (so Groovy 1.8, I assume)环境是 Grails 2.1.1(所以我假设是 Groovy 1.8)

It seems to me that your XML builder is trying to reference some String variable in the environment.在我看来,您的 XML 构建器正试图引用环境中的某些String变量。 Since groovy builder intercept missing method calls, if they find a reference they will try to apply to it.由于 groovy builder 拦截丢失的方法调用,如果他们找到引用,他们将尝试应用到它。 The following code can reproduce your error:以下代码可以重现您的错误:

def id = ""

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

And the following is fine:以下是好的:

def ids = ""

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

Renaming your id variable should do the trick重命名您的id变量应该可以解决问题


Update:更新:

An alternative way to use a tag with same name as a variable in the scope is with a (ugly) GString:使用与作用域中的变量同名的标签的另一种方法是使用(丑陋的)GString:

def id = ""

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

run into the same situation and qualify it with the builder solved the problem for me.遇到同样的情况并与建造者进行资格鉴定,为我解决了这个问题。

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.
                }
            }
        }
    }

I would like to do the same as MarkupBuilder for Groovy but in java do you know of a class or solution to do this the same way?我想对 Groovy 执行与 MarkupBuilder 相同的操作,但是在 Java 中,您是否知道有一个类或解决方案可以以相同的方式执行此操作? Thx !谢谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM