使用Gradle中的特定ID更新xml元素 [英] Update xml elements with specific id in gradle

查看:143
本文介绍了使用Gradle中的特定ID更新xml元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在xml文件中进行以下更改



我的test.xml

 <根> 
< display-name>部分名称< / display-name>
....
< resource-ref id ='change'>
< resource-name>更改此< / resource-name>
< / resource-ref>
< resource-ref id ='modify'>
< resource-name>也要更改< / resource-name>
< / resource-ref>
< / root>

我想在这个xml文件中改变看起来像这样

 < root> 
< display-name>最终名称< / display-name>
....
< resource-ref id ='change'>
< resource-name>变更后< / resource-name>
< / resource-ref>
< resource-ref id ='modify'>
< resource-name>修改后< / resource-name>
< / resource-ref>
< / root>

第一个答案在这个问题几乎回答了我的问题。但我需要对具有特定ID的元素进行特定更改,如您所见。



这看起来很简单。我试着寻找答案,但没有找到答案。任何帮助表示赞赏。



btw我的gradle脚本看起来像这样

  task(replace)<< {
def xmlSource = file(path / to / test.xml)
def xmlDest = file(path / to / destination)
def xmlParser = new XmlParser()
xmlParser.setFeature(http://apache.org/xml/features/disallow-doctype-decl,false)
def xmlRoot = xmlParser.parse(xmlSource)

xmlRoot.'display-name'[0] .value ='RTM16'
//寻找像这样的东西
//xmlRoot.'resource-ref'[@id ='change']。'resource-name'[0] .value ='更改后'
//xmlRoot.'resource-ref'[@id='modify'].'resource-name'[0 ] .value ='修改后'

def nodePrinter = new XmlNodePrinter(new PrintWriter(new FileWriter(xmlDest)))
nodePrinter.preserveWhitespace = true
nodePrinter.print(xmlRoot )
}


解决方案

a href =http://docs.groovy-lang.o rg / latest / html / api / groovy / util / Node.htmlrel =nofollow> Node(Groovy 2.4.6)我想出了这个

 任务(替换)<< {{
xmlSource = file(path / to / xml源文件)
xmlDest = file(path / to / destinationfile)
def parser = new XmlParser()
def xmlRoot = parser.parse(xmlSource)
xmlRoot.each {
if(it.name()。equals(资源引用)&安培;&安培;它。@ id.equals(change)){
it.'resource-name'[0] .value ='变更后'
}
else if(it.name ).equals(resource-ref)& it。@ id.equals(modify)){
it.'resource-name'[0] .value ='修改后'$ b (新的FileWriter(xmlDest)))
b.preserveWhitespace = true
b.print(z)
$ b )
}

不知道它是否是最好的方法。但它可以工作

I want to make the following changes in the xml file

My test.xml

<root>
    <display-name>Some Name</display-name>
    ....
    <resource-ref id='change'>
        <resource-name>Change this</resource-name>
    </resource-ref>
    <resource-ref id='modify'>
        <resource-name>Change this too</resource-name>
    </resource-ref>
</root>

I want to make changes in this xml file to look like this

<root>
    <display-name>Final Name</display-name>
    ....
    <resource-ref id='change'>
        <resource-name>After Change</resource-name>
    </resource-ref>
    <resource-ref id='modify'>
        <resource-name>After Modify</resource-name>
    </resource-ref>
</root>

The first answer in this question nearly answers my question. But I need to make specific changes for elements with specific id as you can see.

This looks very simple. I tried looking answers and failed to find. Any help is appreciated.

And btw my gradle script looks like this

task ("replace")<<{
def xmlSource = file(path/to/test.xml)
def xmlDest = file(path/to/destination)
def xmlParser = new XmlParser()
xmlParser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
def xmlRoot = xmlParser.parse(xmlSource)

xmlRoot.'display-name'[0].value = 'RTM16'
//Looking for something like this
//xmlRoot.'resource-ref'[@id='change'].'resource-name'[0].value = 'After Change'
//xmlRoot.'resource-ref'[@id='modify'].'resource-name'[0].value = 'After Modify'

def nodePrinter = new XmlNodePrinter(new PrintWriter(new FileWriter(xmlDest)))
nodePrinter.preserveWhitespace = true
nodePrinter.print(xmlRoot)
}

解决方案

After going through the Node (Groovy 2.4.6) I came up with this

task ("replace")<<{
xmlSource = file(path/to/xml source file)
xmlDest = file(path/to/destinationfile)
def parser = new XmlParser()
def xmlRoot = parser.parse(xmlSource)
xmlRoot.each{
    if(it.name().equals("resource-ref")&& it.@id.equals("change")){
        it.'resource-name'[0].value = 'After Change'
    }
    else if(it.name().equals("resource-ref")&& it.@id.equals("modify")){
        it.'resource-name'[0].value = 'After Modify'
    }
}

def b = new XmlNodePrinter(new PrintWriter(new FileWriter(xmlDest)))
b.preserveWhitespace = true
b.print(z)
}

Not sure if its the best way. But it works

这篇关于使用Gradle中的特定ID更新xml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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