我有這樣的代碼:Groovy的MarkupBuilder的名稱衝突
String buildCatalog(Catalog catalog) {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.catalog(xmlns:'http://www.sybrium.com/XMLSchema/NodeCatalog') {
'identity'() {
groupId(catalog.groupId)
artifactId(catalog.artifactId)
version(catalog.version)
}
}
return writer.toString();
}
它生產這個XML:
<catalog xmlns='http://www.sybrium.com/XMLSchema/NodeCatalog'>
<groupId>sample.group</groupId>
<artifactId>sample-artifact</artifactId>
<version>1.0.0</version>
</catalog>
注意,「身份」標籤丟失......我已經在嘗試了一切世界去獲得該節點的出現。我把我的頭髮撕掉了!
在此先感謝。
這有效,但你能解釋一下嗎?什麼是委託,爲什麼delegate.identity與delegate.invokeMethod('identity')不同? – 2012-07-09 04:32:15
我想通了,我會更新我的答案。 – OverZealous 2012-07-09 07:02:34
僅供參考:我通過使用GroovyConsole檢查XML對象來追蹤此情況。這讓我知道'identity'方法已經存在,只有一個'Closure'作爲它的參數。 – OverZealous 2012-07-09 07:08:40