Groovy: copy XML elements from one doc to another

折月煮酒 提交于 2019-12-05 20:50:43

You can do this with XmlSlurper:

import groovy.xml.*

def pxml = '''<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>
             |</project>'''.stripMargin()

def p = new XmlSlurper().parseText( pxml )

String nxml = new StreamingMarkupBuilder().bind {
  project {
    dependecyManagement {
      dependencies {
        mkp.yield p.dependencyManagement.dependencies.children()
      }
    }
  }
}

println XmlUtil.serialize( nxml )

Which prints:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <dependecyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependecyManagement>
</project>

To handle the namespaces better, you could try:

def pxml = '''<project xmlns="http://maven.apache.org/POM/4.0.0"
             |         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             |         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
             |  <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>
             |</project>'''.stripMargin()

def p = new XmlSlurper().parseText( pxml )

String nxml = new StreamingMarkupBuilder().bind {
  mkp.declareNamespace(    '':"http://maven.apache.org/POM/4.0.0",
                        'xsi':"http://www.w3.org/2001/XMLSchema-instance" )
  project( 'xsi:schemaLocation':p.@schemaLocation ) {
    dependecyManagement {
      dependencies {
        mkp.yield p.dependencyManagement.dependencies.children()
      }
    }
  }
}

println XmlUtil.serialize( nxml )

Which should give you:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <dependecyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependecyManagement>
</project>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!