Reputation: 7843
Following on from my last post here, where I was shown how to read an XML attribute, my last task is now to set the attribute, e.g. I'll be incrementing the read attribute and then writing back.
So, again I have the following XML file:
<?xml version='1.0' encoding='utf-8'?>
<widget android-versionCode="16" id="com.mycomp.myapp" ios-CFBundleVersion="15" version="1.3.0.b4" windows-packageVersion="1.2.6.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>My App</name>
<description>My app description</description>
<author>mycom.com.au</author>
And I learnt that the following, as per the help did not work:
def xml = readFile "${env.WORKSPACE}/config.xml"
def rootNode = new XmlParser().parseText(xml)
def version = rootNode.@version
but the following would:
def version = rootNode.attributes()['version']
I now seem to have the same problem writing the attribute back.
Following this post I tried the following to set the attribute:
def filePath = "${env.WORKSPACE}/config.xml"
def xml = readFile filePath
def rootNode = new XmlParser().parseText(xml)
rootNode.@version = "12345"
def writer = new FileWriter(filePath)
new XmlNodePrinter(new PrintWriter(writer)).print(rootNode)
But I get a similar error to when I was trying to read the attribute:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node version
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onSetAttribute(SandboxInterceptor.java:447)
at org.kohsuke.groovy.sandbox.impl.Checker$9.call(Checker.java:405)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedSetAttribute(Checker.java:411)
I did try this in Groovy playground, and it did seem to work, however not here in Jenkins.
So, it looks once again the .@version
syntax is not working, and I just cannot fins an alternative call (like there was to get the attribute) to set the attribute.
How can I do this?
Upvotes: 0
Views: 1735
Reputation: 3075
After some more testing i found out, we can simply use the @
selector inside the []
access (Edit: Its called map notation), seems the script sandbox can handle this. It translates in getAt()
and putAt()
under the hood which jenkins will allow to be approved.
node() {
def xml = readFile "${env.WORKSPACE}/config.xml"
def rootNode = new XmlParser().parseText(xml)
print rootNode['@version']
rootNode['@version'] = 123
print rootNode['@version']
}
Result
Running on Jenkins in /var/jenkins_home/workspace/xmltest
[Pipeline] {
[Pipeline] readFile
[Pipeline] echo
1.3.0.b4
[Pipeline] echo
123
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Upvotes: 2