Search code examples
jenkinsgroovyxmlslurper

Jenkins: Using XmlSlurper In Declarative Pipeline


Using XmlSlurper, I am trying to read an XML file (specifically Web.config from a .Net-based API) as part of a Jenkins pipeline. I do not seem to be able to access any attributes of elements. The error I get is:

No such field found: field groovy.util.slurpersupport.NodeChild primary

Below is my attempt to break this down into the simplest case:

script {
    def xml = """
              <colors>
                  <color primary="true">Red</color>
                  <color primary="true">Yellow</color>
                  <color primary="true">Blue</color>
                  <color primary="false">Purple</color>
              </colors>                    
              """

    def colors = new XmlSlurper().parseText(xml)
    echo "First Color: ${colors.color[0]}" //works fine
    echo "First Color: ${colors.color[0]} Primary? ${colors.color[0].@primary}" //fails

}

I am using Jenkins 2.121.1.

Any help is appreciated.


Solution

  • Try changing ${colors.color[0].@primary} to ${colors.color[0]['@primary']}