Reputation: 401
I have simple xml file, that I need to parse in pipeline.
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<AS Name="123">
<Config Name="Configuration1">
<Servers>
<Server Name="server1"/>
<Server Name="server2"/>
<Server Name="server3"/>
<Server Name="server4"/>
</Servers>
</Config>
</AS>
I need to extract values of all servers' names into variable. So I used code below but get only "unclassified field java.lang.string Config" exception
deleteDir()
stage("upload") {
def inputFile = input message: 'Upload file', parameters: [file(name: 'parameters.xml')]
new hudson.FilePath(new File("$workspace/parameters.xml")).copyFrom(inputFile)
inputFile.delete()
}
stage ("parse")
{
def xml = readFile "${env.WORKSPACE}/parameters.xml"
def config = new XmlParser().parseText(xml)
config.Config.Servers.Server.each{
println it.@Name
}
}
XMLSlurper didn't help too. What am I doing wrong?
UPD: rewrite pipeline using noncps but it didn't give the result
node
{
deleteDir()
stage("upload") {
def inputFile = input message: 'Upload file', parameters: [file(name: 'parameters.xml')]
new hudson.FilePath(new File("$workspace/parameters.xml")).copyFrom(inputFile)
inputFile.delete()
}
stage("checkout") {
echo fileExists('parameters.xml').toString()
}
stage ("parse")
{
def xml = readFile "${env.WORKSPACE}/parameters.xml"
println servers(xml)
}
}
@NonCPS
def servers(xmlfile) {
def serv = [:]
def config = new XmlParser().parseText(xmlfile)
config.Config.Servers.Server.each{
serv.add(it.@name)
}
return serv
}
Error ouput now is:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field groovy.util.Node Config
Upvotes: 1
Views: 12248
Reputation: 361
As mentioned in comments here https://issues.jenkins-ci.org/browse/JENKINS-37398 try to use map syntax instead of property syntax.
it['Name']
instead of ...
it.@Name
It worked for me.
Upvotes: 2
Reputation: 401
It seems that my problem is bug of the script-security plugin. https://issues.jenkins-ci.org/browse/JENKINS-37398
Upvotes: 0
Reputation: 28634
the following code
def xml = '''
<AS Name="123">
<Config Name="Configuration1">
<Servers>
<Server Name="server1"/>
<Server Name="server2"/>
<Server Name="server3"/>
<Server Name="server4"/>
</Servers>
</Config>
</AS>
'''
def config = new XmlParser().parseText(xml)
config.Config.Servers.Server.each{
println it.@Name
}
prints
server1
server2
server3
server4
for jenkins pipeline
assume you have this the parameters.xml
note that in the question you have wrong xml: The standalone document declaration value must be "yes" or "no", not "true"
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AS Name="123">
<Config Name="Configuration1">
<Servers>
<Server Name="server1"/>
<Server Name="server2"/>
<Server Name="server3"/>
<Server Name="server4"/>
</Servers>
</Config>
</AS>
the following pipeline prints server names:
@NonCPS
def servers(xml) {
new XmlParser().parseText(xml).Config.Servers.Server.collect{it.@Name}
}
node{
def xml = readFile "${env.WORKSPACE}/parameters.xml"
println servers(xml)
}
Upvotes: 3