Groovy: copy XML elements from one doc to another

ugly

New Member
I am new to Groovy and am stuck with a simple problem. All I wanna do is extract certain elements from one XML file and created a new file with it. Here's an example XML, let's use a Maven pom file:\[code\]<project> <modelVersion>4.0.0</modelVersion> <groupId>com.group</groupId> <artifactId>artifact</artifactId> <version>1.4</version> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement>\[/code\]I know how to parse XML in Groovy:\[code\]def project = new XmlParser().parse("pom.xml")project.groupId.each{ println it.text()}\[/code\]I also know how to create XML in Groovy:\[code\]def xml = new groovy.xml.MarkupBuilder()xml.project (){ modelVersion("artifactId") groupId("com.group") artifactId("artifact")}\[/code\]However, I seem to have a problem combining the two. I want, for example, take groupId, artifactId and the whole dependencies tree and create a new XML from it.It can't be that hard and I wanna make use of the Groovy simplicity.Something along those lines (of course this does not work):\[code\]def newXml= new groovy.xml.MarkupBuilder()newXml.groupId= project.groupIdnewXml.dependencies = project.dependencyManagement.dependencies\[/code\]
 
Back
Top