Jenkins XmlParser 报告没有为根节点的属性找到这样的字段

pet*_*erc 2 jenkins cordova jenkins-pipeline

我有一个项目,其中包含以下 XML 文件(Cordova 项目的 config.xml)...

    <?xml version='1.0' encoding='utf-8'?>
    <widget android-versionCode="16" id="com.mycomp.myapp" ios-CFBundleVersion="15" version="1.3.0.b4" windows-packageVersion="1.2.6.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
        <name>My App</name>
        <description>My app description</description>
        <author>mycom.com.au</author>
           ....
Run Code Online (Sandbox Code Playgroud)

我想要做的就是读取根元素 ( )的version属性值(给我字符串)。按照此处的示例,它说使用来获取属性。1.3.0.b4widget.@

我的 Jenkins 文件脚本中有以下内容...

        script {
              def xml = readFile "${env.WORKSPACE}/config.xml"
              def rootNode = new XmlParser().parseText(xml)
              def version = rootNode.@version
              echo 'version is...'
              echo version
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我收到以下错误..

        org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node version
        at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
        at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetAttribute(SandboxInterceptor.java:436)
        at org.kohsuke.groovy.sandbox.impl.Checker$8.call(Checker.java:370)
        at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetAttribute(Checker.java:375)
        at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getAttribute(SandboxInvoker.java:37)
Run Code Online (Sandbox Code Playgroud)

我已经尝试过rootNode.@version(如上)rootNode[0].@versionrootNode[3].@version但没有任何效果。

有没有人知道上面有什么问题?

提前致谢

[编辑1]

如果我使用以下...

def xml = readFile "${env.WORKSPACE}/config.xml"
def rootNode = new XmlParser().parseText(xml)
def version = rootNode.text()
echo 'version is...'
echo version
Run Code Online (Sandbox Code Playgroud)

它打印出来My app description有点奇怪(它跳到描述节点)

[编辑2]

我尝试使用以下..

 def rootNode = new XmlSlurper().parse("${env.WORKSPACE}/config.xml")
 def version = rootNode.@'version'
Run Code Online (Sandbox Code Playgroud)

但我仍然收到类似的错误...

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.slurpersupport.NodeChild version
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetAttribute(SandboxInterceptor.java:436)
at org.kohsuke.groovy.sandbox.impl.Checker$8.call(Checker.java:370)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetAttribute(Checker.java:375)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getAttribute(SandboxInvoker.java:37)
at com.cloudbees.groovy.cps.impl.AttributeAccessBlock.rawGet(AttributeAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:15)
at ___cps.transform___(Native Method)
Run Code Online (Sandbox Code Playgroud)

如果我调用echo rootNode.text(),它似乎只是打印出主,widget标签中前 3 个标签的内容,即My AppMy app descriptionmycom.com.au.

Dom*_*art 7

编辑:

我在能够修改属性的上下文中进行了更多测试,并发现在使用[]访问时,@属性选择器实际上可以工作。这似乎导致在幕后使用不同的方法,您可以在 jenkins ( getAtand putAt) 中批准这些方法。

我们可以简单地使用

def rootNode = new XmlParser().parseText(xml)
println rootNode['@version']
Run Code Online (Sandbox Code Playgroud)

原答案:

似乎有一些关于使用脚本沙箱@使用groovy.util.Node对象上的选择器直接访问属性的错误。

一种解决方法是使用该.attributes()方法获取属性的完整 Map,并通过如下所示的键访问该值:

def rootNode = new XmlParser().parseText(xml)
println rootNode.attributes()['version']
Run Code Online (Sandbox Code Playgroud)

这将在第一次运行时失败并提示您批准使用method groovy.util.Node attributes,但一旦批准将起作用。