【问题标题】:Groovy - How to transfer XML nodes between documents?Groovy - 如何在文档之间传输 XML 节点?
【发布时间】:2009-08-27 02:16:59
【问题描述】:

%subj%,我试过了:

def xp = new XmlParser(); def testsuite = xp.parseText(""); def testsuite1 = new XmlParser().parse("testsuite.xml"); testsuite1.testcase.each { testsuite.append(它); }

但这给了我一个例外:

groovy.lang.MissingMethodException:没有方法签名:groovy.util.Node.append() 适用于参数类型:(groovy.util.Node) 值:{testcase ..., ... }

尽管:http://groovy.codehaus.org/api/groovy/util/Node.html 说:boolean append(Node child)

那么,如何在文档之间复制/移动节点? (以 Groovy 的方式 - 不使用 W3D DOM / JDOM...)

谢谢, 翁德拉

【问题讨论】:

标签: xml groovy


【解决方案1】:

下面的作品,我猜到 testsuite.xml 的内容可能是什么样子。您的文件很可能是问题所在。

def ts = "<testsuite/>"
def ts1 = """
<testsuite>
  <testcase>
    <foo>bar</foo>
  </testcase>
  <testcase>
    <foo>baz</foo>
  </testcase>
</testsuite>
""".trim()

def testsuite = new XmlParser().parseText(ts)
def testsuite1 = new XmlParser().parseText(ts1)

testsuite1.testcase.each {
  testsuite.append(it);
}

assert "bar" == testsuite.testcase[0].foo.text()
assert "baz" == testsuite.testcase[1].foo.text()

【讨论】:

  • 这真的适合你吗?不适合我......仍然是同样的错误。
  • $ groovy -version Groovy 版本:JVM:14.0-b16 $ groovy xmlText.gy 捕获:groovy.lang.MissingMethodException:没有方法签名:groovy.util.Node.append() 适用于参数类型:(groovy.util.Node) 值:{testcase[attributes={};价值=[foo[属性={}; value=[bar]]]]} at xmlText$_run_closure1.doCall(xmlText.gy:17) at xmlText.run(xmlText.gy:16) at xmlText.main(xmlText.gy)
  • Groovy 1.6.4、Linux、Sun JDK 1.6
  • John 的代码对我有用 - Groovy 1.6.3、Windows XP、Sun JDK 1.6.0_16
  • 我尝试了在 Linux (Ubuntu 9.04 x64) 和 Windows (Vista x64) 上使用 Groovy 1.6.4/Sun JDK 1.6.0_14 发布的代码。我还在在线 Groovy 控制台 groovyconsole.appspot.com 上尝试过,它也可以在那里工作。
猜你喜欢
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
相关资源
最近更新 更多