0

How do I parse xml file in a pipeline job?

Hi,

How do I parse xml file in a pipeline job?

This is what I tried: I have an XML file I want to parse, looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<remote name="origin" fetch="XXX" review="YYYY" />
<default remote="origin" />
<project path="home/projectA" name="projectA" revision="master" groups="A"/>
<project path="home/projectB" name="projectB" revision="master" groups="A"/>
<project path="some_folder/projectC" name="projectC" revision="master" groups="B"/>
<project path="some_folder/projectD" name="projectD" revision="master" groups="C"/>
</manifest>

I want to parse this file in order to get the groups element values.
My code:

git branch: "master", credentialsId: "<>", url: "<manifest repository>"
def xml = readFile(encoding: 'UTF-8', file: "manifest.xml")
def parsed_manifest = new XmlParser().parseText(xml)

clusters = []
parsed_manifest.project.each {
values = it.attribute("groups").split(',')
for (item in values) {
if (item.startsWith("cluster")) {
clusters.add(item)
}
}
}

I run it as a Jenkins file that is being loaded from my git repository and not as a script written in the job's configuration (running the script when written in the job configuration, passes successfully.)

The error I get:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field groovy.util.Node project

(https://issues.jenkins-ci.org/browse/JENKINS-37398 - a similar error)

Please help :)

1 comment

  • 0
    Avatar
    Shiran Honig

    Hi,

    I managed to solve it!

    Using what is written in https://jenkins.io/blog/2017/02/01/pipeline-scalability-best-practice/ about Common non-Serializable Types and Iterators:
    "this is a common problem. You need to use C-style loop, i.e. for(int i=0; i<max; i++){ "

    def xml = readFile(encoding: 'UTF-8', file: "manifest.xml")
    def parsed_manifest = new XmlParser().parseText(xml)
    clusters = []
    for (proj in parsed_manifest['project']){
    groups = proj.attribute("groups").split(',')
    for (item in groups) {
    if (item.startsWith("cluster")) {
    clusters.add(item)
    }
    }
    }
    Edited by Shiran Honig
Please sign in to leave a comment.