在Groovy中修改现有的xml文件 [英] Modify existing xml file in Groovy
本文介绍了在Groovy中修改现有的xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个pom.xml文件,例如:
<?xml version =1.0encoding =utf- 8\" >?;
< project xmlns =http://maven.apache.org/POM/4.0.0>
< modelVersion> 4.0.0< / modelVersion>
< groupId> com.test.plugins< / groupId>
< artifactId> testplugin< / artifactId>
< packaging> grails-plugin< / packaging>
< version> 1.0< / version>
< name> testplugin< / name>
< description> testplugin< / description>
<依赖关系>
< dependency>
< groupId> org.grails< / groupId>
< artifactId> grails-plugin-async< / artifactId>
< version> $ {grails.version}< / version>
< /依赖关系>
< /依赖关系>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-plugin< / artifactId>
<配置>
< skip> true< / skip>
< / configuration>
< / plugin>
< / plugins>
< / build>
< repositories>
< repository>
< id> grails< / id>
<名称> grails< /名称>
< url> http://repo.grails.org/grails/core< / url>
< / repository>
< / repositories>
< / project>
我需要使用groovy xml slurper在上述xml文件中添加更多节点。例如:想要在现有的xml文件中添加下面的配置。
< pluginRepositories>
< pluginRepository>
< id> synergian-repo< / id>
< url> https://raw.github.com/synergian/wagon-git/releases< / url>
< / pluginRepository>
< / pluginRepositories>
同样,我想在依赖关系中添加新的依赖关系元素元素:
<依赖项>
< groupId> org.grails.plugins< / groupId>
< artifactId> tomcat< / artifactId>
< version> 7.0.42< / version>
< type> zip< / type>
< scope>提供< / scope>
< /依赖关系>
我该如何做到这一点?我看了一些例子&如下所示:
new File(pom.xml)。withWriter('UTF-8'){w - > ;
def xml = new MarkupBuilder(w)
xml.pluginRepositories {
pluginRepository {
idsynergian-repo
urlhttps://raw.github。 com / synergian / wagon-git / releases
}
}
}
但上面的代码只用新文本替换了整个文件。
import groovy.xml.XmlUtil
def pom ='''
<?xml version = 1.0encoding =utf-8?>
< project xmlns =http://maven.apache.org/POM/4.0.0>
< modelVersion> 4.0.0< / modelVersion>
< groupId> com.test.plugins< / groupId>
< artifactId> testplugin< / artifactId>
< packaging> grails-plugin< / packaging>
< version> 1.0< / version>
< name> testplugin< / name>
< description> testplugin< / description>
<依赖关系>
< dependency>
< groupId> org.grails< / groupId>
< artifactId> grails-plugin-async< / artifactId>
< version> $ {grails.version}< / version>
< /依赖关系>
< /依赖关系>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-plugin< / artifactId>
<配置>
< skip> true< / skip>
< / configuration>
< / plugin>
< / plugins>
< / build>
< repositories>
< repository>
< id> grails< / id>
<名称> grails< /名称>
< url> http://repo.grails.org/grails/core< / url>
< / repository>
< / repositories>
< / project>
'''
def xml = new XmlSlurper(false,false).parseText(pom)
xml.appendNode {
pluginRepositories {
pluginRepository {
id'synergian-repo'
url'https://raw.github.com/synergian/wagon-git/releases'
}
}
}
xml.dependencies.appendNode {
dependency {
groupId'org.grails.plugins'
artifactId'tomcat'
version'7.0.42'
type 'zip'
scope'provided'
}
}
XmlUtil.serialize(xml)
I have a pom.xml file like:
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.plugins</groupId>
<artifactId>testplugin</artifactId>
<packaging>grails-plugin</packaging>
<version>1.0</version>
<name>testplugin</name>
<description>testplugin</description>
<dependencies>
<dependency>
<groupId>org.grails</groupId>
<artifactId>grails-plugin-async</artifactId>
<version>${grails.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>grails</id>
<name>grails</name>
<url>http://repo.grails.org/grails/core</url>
</repository>
</repositories>
</project>
I need to add some more nodes in the above xml file using groovy xml slurper. For example: wants to add the below configuration inside the existing xml file.
<pluginRepositories>
<pluginRepository>
<id>synergian-repo</id>
<url>https://raw.github.com/synergian/wagon-git/releases</url>
</pluginRepository>
</pluginRepositories>
Similarly, I want to add new dependency element inside dependencies element:
<dependency>
<groupId>org.grails.plugins</groupId>
<artifactId>tomcat</artifactId>
<version>7.0.42</version>
<type>zip</type>
<scope>provided</scope>
</dependency>
How can I achieve this? I looked at some examples & written code like:
new File("pom.xml").withWriter('UTF-8') { w ->
def xml = new MarkupBuilder(w)
xml.pluginRepositories {
pluginRepository {
id "synergian-repo"
url "https://raw.github.com/synergian/wagon-git/releases"
}
}
}
But the above code replaces the whole file with only new text.
解决方案
Here You go:
import groovy.xml.XmlUtil
def pom = '''
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.plugins</groupId>
<artifactId>testplugin</artifactId>
<packaging>grails-plugin</packaging>
<version>1.0</version>
<name>testplugin</name>
<description>testplugin</description>
<dependencies>
<dependency>
<groupId>org.grails</groupId>
<artifactId>grails-plugin-async</artifactId>
<version>${grails.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>grails</id>
<name>grails</name>
<url>http://repo.grails.org/grails/core</url>
</repository>
</repositories>
</project>
'''
def xml = new XmlSlurper(false,false).parseText(pom)
xml.appendNode {
pluginRepositories {
pluginRepository {
id 'synergian-repo'
url 'https://raw.github.com/synergian/wagon-git/releases'
}
}
}
xml.dependencies.appendNode {
dependency {
groupId 'org.grails.plugins'
artifactId 'tomcat'
version '7.0.42'
type 'zip'
scope 'provided'
}
}
XmlUtil.serialize(xml)
这篇关于在Groovy中修改现有的xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文