user3736941
user3736941

Reputation: 73

Jenkins...Modify XML Tag value in xml file using Groovy in Jenkins

I am using jenkins for automated deployment.

I needs to modify xml tag value in xml file using groovy script. I am using below groovy code. When I try to edit xml tag value I am receiving error unclassified field xml.uti.node error.

Node xml = xmlParser.parse(new File("c:/abc/test.xml"))
xml.DeployerServer.host[0] = '172.20.204.49:7100'
FileWriter fileWriter = new FileWriter("c:/abc/test.xml")
XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(fileWriter))
nodePrinter.setPreserveWhitespace(true)
nodePrinter.print(xml)

I need to modify host tag value and host is available inside DeployerServer tag.

Any help will be much appreciated.

Upvotes: 5

Views: 9288

Answers (3)

From Orbonia
From Orbonia

Reputation: 676

I was wanting to read / manipulate the CSProj file and NUSPEC files in a Pipeline script. I could not get passed the parseText() without the dreaded "SAXParseException: Content is not allowed in prolog".

There are quite a few threads about this error message. What wasn't clear is that both CSProj and NUSPEC files are UTF-8 with BOM - BUT this is invisible!

To make it worse I've been trying to automate the NUSPEC file creation, and there is no way I can tell the tools to change file encoding. The answers above helped solve my issue, and once I added code to look for 65279 as the first character (and deleted it). I could then parse the XML and carry out the above.

There didn't seem to be good thread to put this summary on, so added it to a thread about Jenkins, Groovy & XML files which is where I found this "known Java" issue.

Upvotes: 2

Matheus
Matheus

Reputation: 21

I used powershell to do this change in app.config file. My problem was with passwords. So, I created a Credential, in jenkins, to store the password. If you do not need to work with credential, just remove the withCredentials section

Here is part of my jenkinsfile:

def appConfigPath = "\\server\folder\app.config"

stage('Change App.Config'){
    steps{
        withCredentials([string(credentialsId: 'CREDENTIAL_NAME', variable: 'PWD')]) {
            powershell(returnStdout: true, script: '''
                Function swapAppSetting {
                    param([string]$key,[string]$value )
                        $obj = $doc.configuration.appSettings.add | where {$_.Key -eq $key }
                    $obj.value = $value
                }

                $webConfig = "'''+appConfigPath+'''"

                $doc = [Xml](Get-Content $webConfig)

                swapAppSetting 'TAG_TO_MODIFY' 'VALUE_TO_CHANGE'

                $doc.Save($webConfig)
            ''')
        }
    }
}

Don`t forget to update your powershell. (minimum version 3)

Upvotes: 1

Rao
Rao

Reputation: 21369

Here is the script, comments in-line:

//Create file object
def file = new File('c:/abc/test.xml')
//Parse it with XmlSlurper
def xml = new XmlSlurper().parse(file)
//Update the node value using replaceBody
xml.DeployerServer.host[0].replaceBody '172.20.204.49:7100'
//Create the update xml string
def updatedXml = groovy.xml.XmlUtil.serialize(xml)
//Write the content back
file.write(updatedXml)

Upvotes: 8

Related Questions