Groovy:将XML文档从一个文档复制到另一个文档 [英] Groovy: copy XML elements from one doc to another

查看:350
本文介绍了Groovy:将XML文档从一个文档复制到另一个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Groovy的新手,并且陷入了一个简单的问题。我想要做的就是从一个XML文件中提取某些元素,并用它创建一个新文件。下面是一个XML示例,让我们使用Maven pom文件:

 < project> 
< modelVersion> 4.0.0< / modelVersion>
< groupId> com.group< / groupId>
< artifactId>工件< / artifactId>
< version> 1.4< / version>
< dependencyManagement>
<依赖关系>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.8.2< / version>
< scope> test< / scope>
< /依赖关系>
< /依赖关系>
< / dependencyManagement>



我知道如何解析XML在Groovy中:
$ b $ pre $ def project = new XmlParser()。parse(pom.xml)
project.groupId .each {
println it.text()
}

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

  def xml = new groovy.xml.MarkupBuilder()
xml.project ){
modelVersion(artifactId)
groupId(com.group)
artifactId(artifact)
}

然而,我认为将这两者结合起来存在问题。例如,我想要 groupId artifactId 和整个依赖关系树,并从中创建一个新的XML。
这不是那么难,我想要使用Groovy的简单性。



有些东西(当然这不行): / p>

  def newXml = new groovy.xml.MarkupBuilder()
newXml.groupId = project.groupId
newXml .dependencies = project.dependencyManagement.dependencies






谢谢。这段代码有很多帮助,但是我该如何处理名称空间,即如果输入中的项目标签如下所示:

 << ; project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http:/ /maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"> 

然后在输出中添加一些奇怪的注释。我想要的就是输出中的项目标签也是这样。 你可以用 XmlSlurper

  import groovy.xml。* 

def pxml ='''< project>
| < modelVersion> 4.0.0< / modelVersion>
| <&的groupId GT; com.group< /&的groupId GT;
| < artifactId的>伪影< / artifactId的>
| <版本> 1.4< /版本>
| < dependencyManagement>
| <依赖性>
| <依赖性>
| <&的groupId GT; junit的< /&的groupId GT;
| < artifactId的> junit的< / artifactId的>
| <版本> 4.8.2< /版本>
| <范围>试验< /范围>
| < /依赖性>
| < /依赖性>
| < / 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)



 <?xml version =1.0encoding =UTF-8?> 
< project>
< dependecyManagement>
<依赖关系>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.8.2< / version>
< scope> test< / scope>
< /依赖关系>
< /依赖关系>
< / dependecyManagement>
< / project>

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

  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 GT; com.group< /&的groupId GT;
| < artifactId的>伪影< / artifactId的>
| <版本> 1.4< /版本>
| < dependencyManagement>
| <依赖性>
| <依赖性>
| <&的groupId GT; junit的< /&的groupId GT;
| < artifactId的> junit的< / artifactId的>
| <版本> 4.8.2< /版本>
| <范围>试验< /范围>
| < /依赖性>
| < /依赖性>
| < / 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 ('xsi:schemaLocation':p。@ schemaLocation){
dependecyManagement {
dependencies {
mkp.yield p.dependencyManagement .dependencies.children()
}
}
}
}

println XmlUtil.serialize(nxml)
 < / p> 

> ;?xml version =1.0encoding =UTF-8?>
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
< dependecyManagement>
<依赖关系>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.8.2< / version>
< scope> test< / scope>
< /依赖关系>
< /依赖关系>
< / dependecyManagement>
< / project>


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:

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

I know how to parse XML in Groovy:

def project = new XmlParser().parse("pom.xml")
project.groupId.each{
  println it.text()
}

I also know how to create XML in Groovy:

def xml = new groovy.xml.MarkupBuilder()
xml.project (){
  modelVersion("artifactId")
  groupId("com.group")
  artifactId("artifact")
}

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

def newXml= new groovy.xml.MarkupBuilder()
newXml.groupId= project.groupId
newXml.dependencies = project.dependencyManagement.dependencies


Thanks. That code helped a lot but how can I handle the namespace, i.e. if the project tag in the input looks like that:

<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">

It is then adding some weird annotations to the output. All I want is that the project tag in the output looks like that as well.

解决方案

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>

这篇关于Groovy:将XML文档从一个文档复制到另一个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆