Update xml elements with specific id in gradle

前端 未结 1 1175
遥遥无期
遥遥无期 2021-01-23 18:42

I want to make the following changes in the xml file

My test.xml


    Some Name
    ....
    &l         


        
相关标签:
1条回答
  • 2021-01-23 19:12

    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

    0 讨论(0)
提交回复
热议问题