Jenkins groovy pipeline groovy.lang.MissingPropertyException: No such field found: field groovy.util.Node element1

I am parsing the below xml content using the XmlParser class in groovy.
The content of xml is as follows:

<root><element1 attribute1="value1">Text content of element1</element1></root>

And trying to retrieve the text content of the first child node of root node, like this:

output.element1.text()

where, output is the result after parsing the xml content using XmlParser.

Completed code snippet I am running is as follows:

def parser = new XmlParser()
def xmlContent = '<root><element1 attribute1="value1">Text content of element1</element1></root>'
def output = parser.parseText(xmlContent)
script.echo(output.element1.text())

It works fine in my local and gives expected output of script.echo as ‘Text content of element1

However, when I executed this in Jenkins groovy pipeline job, I get the following:

groovy.lang.MissingPropertyException: No such field found: field groovy.util.Node element1

I compared the versions of groovy and java with the version used in Jenkins. But it’s the same version.

In my local:

  • Groovy version - 2.4.21
  • Java version - 11

In Jenkins:

  • Groovy version - 2.4.21
  • Java version - 11
  • Jenkins version - 2.375.1

Do you know what might be the reason for such behavior in Jenkins with XmlParser? How to overcome this?

I don’t think that you can use plain Groovy code in Jenkins, only a subset of Groovy is supported. XML processing and similar complex steps should be done in a specific plugin.

Try using XmlSlurper. There’s a similar question here from some years ago. So not sure if this is still valid.

I tried XmlSlurper as well, as described here (The Apache Groovy programming language - Processing XML)

def text = '''
    <list>
        <technology>
            <name>Groovy</name>
        </technology>
    </list>
'''

def list = new XmlSlurper().parseText(text) 

println list.technology.name.text()

Using a GPath expression like this list.technology.name.text() works fine but requires an admin to allow “method groovy.lang.GroovyObject getProperty java.lang.String” which the script security plugin recommends against (“Approving this signature may introduce a security vulnerability! You are advised to deny it.”). Since I am not the owner of the Jenkins instance, I am not sure I can convince them this is OK to approve.

Is there a way to write the code such that it does not depend on GroovyObject.getProperty being permitted? Or is there someway to configure Jenkins so that this can work? Or how to proceed ahead with it?

Is there a way to write the code such that it does not depend on GroovyObject.getProperty being permitted? Or is there someway to configure Jenkins so that this can work? Or how to proceed ahead with it?

Why do you want to parse XML in your pipeline?

Our applications consists of pom.xml file and from that xml file we need to fetch the available information and perform further processing on it. Therefore, we need to parse the XML.

You might use the readMavenPom() step for parsing the pom.
It’s also possible to use mvn CLI, as mentioned here

readMavenPom: Read a maven project file.

Reads a Maven project file. The returned object is a Model .

Avoid using this step and writeMavenPom. It is better to use the sh step to run mvn goals. For example:

def version = sh script: 'mvn help:evaluate -Dexpression=project.version -q -DforceStdout', returnStdout: true

then use the model, i.e.

      pom = readMavenPom(file: "$getPom")
      isMultiModuleBuild = pom.getModules().size() != 0
      version = pom.getVersion()

Don’t know why it has “Avoid using this step”, i use it for years without any problems.

We are using the shared library to enhance our multibranch pipeline.