R – Groovy – How to transfer XML nodes between documents

groovyxml

to the %subj%, I've tried:

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

But this gives me an exception:

groovy.lang.MissingMethodException: No signature of method: groovy.util.Node.append() is applicable for argument types: (groovy.util.Node) values: {testcase …, … }

Despite of: http://groovy.codehaus.org/api/groovy/util/Node.html
says: boolean append(Node child)

So, how do I copy/move nodes between documents? (In a Groovy way – not using W3D DOM / JDOM…)

Thanks,
Ondra

Best Answer

The following works, I guessed as to what the contents of testsuite.xml might look like. It likely that your file is the problem.

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()