Groovy:将XML元素从一个doc复制到另一个doc

Jon*_*nas 3 xml groovy markupbuilder xml-parsing

我是Groovy的新手,我遇到了一个简单的问题.我想做的就是从一个XML文件中提取某些元素并用它创建一个新文件.这是一个示例XML,让我们使用Maven pom文件:

<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>
Run Code Online (Sandbox Code Playgroud)

我知道如何在Groovy中解析XML:

def project = new XmlParser().parse("pom.xml")
project.groupId.each{
  println it.text()
}
Run Code Online (Sandbox Code Playgroud)

我也知道如何在Groovy中创建XML:

def xml = new groovy.xml.MarkupBuilder()
xml.project (){
  modelVersion("artifactId")
  groupId("com.group")
  artifactId("artifact")
}
Run Code Online (Sandbox Code Playgroud)

但是,我似乎把这两者结合起来有问题.例如,我想要使用groupId,artifactId和整个依赖树,并从中创建一个新的XML.它不是那么难,我想利用Groovy的简单性.

沿着这些方向的东西(当然这不起作用):

def newXml= new groovy.xml.MarkupBuilder()
newXml.groupId= project.groupId
newXml.dependencies = project.dependencyManagement.dependencies
Run Code Online (Sandbox Code Playgroud)

谢谢.该代码有很多帮助,但我如何处理命名空间,即输入中的项目标记如下:

<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">
Run Code Online (Sandbox Code Playgroud)

然后它会在输出中添加一些奇怪的注释.我想要的只是输出中的项目标签也是如此.

tim*_*tes 6

你可以这样做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 )
Run Code Online (Sandbox Code Playgroud)

哪个印刷品:

<?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>
Run Code Online (Sandbox Code Playgroud)

要更好地处理命名空间,您可以尝试:

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 )
Run Code Online (Sandbox Code Playgroud)

哪个应该给你:

<?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>
Run Code Online (Sandbox Code Playgroud)