我是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")
}
然而,我似乎把两者结合起来有问题。例如,我想要 groupId , artifactId 和整个依赖项树,并从中创建一个新的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">
然后在输出中添加一些奇怪的注释。我想要的只是输出中的项目标签也是这样。
答案 0 :(得分: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>