3

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

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

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

但是,我似乎在将两者结合起来时遇到了问题。例如,我想获取groupIdartifactId和整个依赖关系树,并从中创建一个新的 XML。它不会那么难,我想利用 Groovy 的简单性。

这些方面的东西(当然这不起作用):

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

谢谢。该代码帮助很大,但我如何处理命名空间,即输入中的项目标记是否如下所示:

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

然后它在输出中添加一些奇怪的注释。我想要的是输出中的项目标签也是这样。

4

1 回答 1

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 )

哪个打印:

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

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

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 )

哪个应该给你:

<?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>
于 2012-12-05T09:58:06.887 回答