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.
Using a shell script within Jenkins Pipeline can help simplify builds by combining multiple steps into a single stage. The shell script also allows users to add or update commands without having to modify each step or stage separately.
Basically, declarative and scripted pipelines differ in terms of the programmatic approach. One uses a declarative programming model and the second uses an imperative programming mode. Declarative pipelines break down stages into multiple steps, while in scripted pipelines there is no need for this.
The @NonCPS annotation is useful when you have methods which use objects which aren't serializable. Normally, all objects that you create in your pipeline script must be serializable (the reason for this is that Jenkins must be able to serialize the state of the script so that it can be paused and stored on disk).
Try changing ${colors.color[0].@primary}
to ${colors.color[0]['@primary']}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With