5

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.

voodoobilly
  • 405
  • 7
  • 18
  • Thanks for the help. Out of curiosity, all my research showed that the syntax I was using should be correct. Do you know if something changed, or did I just miss a nuance of the syntax? – voodoobilly Aug 21 '18 at 11:34

2 Answers2

3

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

yong
  • 13,357
  • 1
  • 16
  • 27
0

disable "Use groovy sandbox".

The source of the problem is code via a Jenkinsfile, which I believe always runs in the sandbox.

Works fine, when its unchecked. :)

rohit thomas
  • 2,302
  • 11
  • 23
  • 1
    Thanks for the answer, which also worked. I chose the accepted answer simply because it avoids any security issues related to running outside the sandbox. – voodoobilly Aug 21 '18 at 11:32