首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jenkinsfile ().parseText(Xml_file)不适用于XmlParser节点名称

Jenkinsfile ().parseText(Xml_file)不适用于XmlParser节点名称
EN

Stack Overflow用户
提问于 2020-01-13 17:43:55
回答 2查看 272关注 0票数 0

从一些XML文件中提取一些值是必需的。从Jenkinsfile中触发提取。下面的代码在我的Jenkinsfile中运行良好:

代码语言:javascript
复制
    def xml_file_contents = new XmlParser().parseText(xml_file)
    def value_i_need_to_extract = xml_file_contents.children()[4].children()[0].children()[0].children()[0].text().toString()

但是,解析的XML文件稍后可能会被修改,因此通过XML节点名而不是通过children()方法进行遍历将是一个更好的主意。然而,每次我应用这样的东西时:

代码语言:javascript
复制
    def value_i_need_to_extract = xml_file_contents.nodename.nodename.nodename.nodename.text().toString()

我收到以下错误:

代码语言:javascript
复制
    org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote

我尝试过使用不同的XML文件和不同的节点名称遍历节点名称,但结果总是错误。

尽管我已经批准了Jenkins进程内脚本审批所需的所有安全功能,但仍会出现此错误。

问题说明示例:

代码A:

代码语言:javascript
复制
def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].children()[0]
echo 'Remote repository is:' + remote_repo.toString()

输出A:

代码语言:javascript
复制
Remote repository is:remote[attributes={}; value=[http://LAPTOP/svn/localrepo/app/component/RC]]

代码B:

代码语言:javascript
复制
def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].children()[0].text()
echo 'Remote repository is:' + remote_repo.toString()

输出B:

代码语言:javascript
复制
Remote repository is:http://LAPTOP/svn/localrepo/app/component/RC

代码C:

代码语言:javascript
复制
def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].remote.text()

输出C:

代码语言:javascript
复制
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote

你知道这种行为的可能原因吗?有没有办法通过节点名遍历XML?

示例XML文件:

代码语言:javascript
复制
    <?xml version='1.1' encoding='UTF-8'?>
    <project>
      <description>Just a sample build job</description>
      <keepDependencies>false</keepDependencies>
      <properties>
        <hudson.plugins.jira.JiraProjectProperty plugin="jira@3.0.11"/>
      </properties>
      <scm class="hudson.scm.SubversionSCM" plugin="subversion@2.12.2">
        <locations>
          <hudson.scm.SubversionSCM_-ModuleLocation>
            <remote>http://LAPTOP/svn/localrepo/subfolder/componentname/RC</remote>
            <credentialsId>justsomeid</credentialsId>
            <local>.</local>
            <depthOption>infinity</depthOption>
            <ignoreExternalsOption>true</ignoreExternalsOption>
            <cancelProcessOnExternalsFail>true</cancelProcessOnExternalsFail>
          </hudson.scm.SubversionSCM_-ModuleLocation>
        </locations>
        <excludedRegions></excludedRegions>
        <includedRegions></includedRegions>
        <excludedUsers></excludedUsers>
        <excludedRevprop></excludedRevprop>
        <excludedCommitMessages></excludedCommitMessages>
        <workspaceUpdater class="hudson.scm.subversion.UpdateUpdater"/>
        <ignoreDirPropChanges>false</ignoreDirPropChanges>
        <filterChangelog>false</filterChangelog>
        <quietOperation>true</quietOperation>
      </scm>
      <canRoam>true</canRoam>
      <disabled>false</disabled>
      <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
      <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
      <triggers/>
      <concurrentBuild>false</concurrentBuild>
      <builders/>
      <publishers>
        <hudson.plugins.ws__cleanup.WsCleanup plugin="ws-cleanup@0.37">
          <patterns class="empty-list"/>
          <deleteDirs>false</deleteDirs>
          <skipWhenFailed>false</skipWhenFailed>
          <cleanWhenSuccess>true</cleanWhenSuccess>
          <cleanWhenUnstable>true</cleanWhenUnstable>
          <cleanWhenFailure>true</cleanWhenFailure>
          <cleanWhenNotBuilt>true</cleanWhenNotBuilt>
          <cleanWhenAborted>true</cleanWhenAborted>
          <notFailBuild>false</notFailBuild>
          <cleanupMatrixParent>false</cleanupMatrixParent>
          <externalDelete></externalDelete>
          <disableDeferredWipeout>false</disableDeferredWipeout>
        </hudson.plugins.ws__cleanup.WsCleanup>
      </publishers>
      <buildWrappers/>
    </project>
EN

回答 2

Stack Overflow用户

发布于 2020-01-13 20:28:15

XmlParser should not be used,因为它不序列化(它只在主节点内工作,主节点不应该有任何执行器)。

不幸的是,Jenkins没有提供像JSON和YAML那样的可序列化步骤(参见Pipeline Utility Steps)。

如果您能够将XML转换为JSON或YAML,那么您将能够适当地使用readJSON/readYaml步骤,并且您也可以更轻松地导航它们。如果这是不可能的,您应该考虑编写一个小实用程序来帮助您处理XML文件:这样,您就可以在不是主节点的代理上运行管道脚本。

票数 0
EN

Stack Overflow用户

发布于 2020-01-14 15:04:56

下面的代码可以在我的Groovy控制台上运行:

代码语言:javascript
复制
def xml_file = """<?xml version='1.1' encoding='UTF-8'?>
    <project>
      <description>Just a sample build job</description>
      <keepDependencies>false</keepDependencies>
      <properties>
        <hudson.plugins.jira.JiraProjectProperty plugin="jira@3.0.11"/>
      </properties>
      <scm class="hudson.scm.SubversionSCM" plugin="subversion@2.12.2">
        <locations>
          <hudson.scm.SubversionSCM_-ModuleLocation>
            <remote>http://LAPTOP/svn/localrepo/subfolder/componentname/RC</remote>
            <credentialsId>justsomeid</credentialsId>
            <local>.</local>
            <depthOption>infinity</depthOption>
            <ignoreExternalsOption>true</ignoreExternalsOption>
            <cancelProcessOnExternalsFail>true</cancelProcessOnExternalsFail>
          </hudson.scm.SubversionSCM_-ModuleLocation>
        </locations>
        <excludedRegions></excludedRegions>
        <includedRegions></includedRegions>
        <excludedUsers></excludedUsers>
        <excludedRevprop></excludedRevprop>
        <excludedCommitMessages></excludedCommitMessages>
        <workspaceUpdater class="hudson.scm.subversion.UpdateUpdater"/>
        <ignoreDirPropChanges>false</ignoreDirPropChanges>
        <filterChangelog>false</filterChangelog>
        <quietOperation>true</quietOperation>
      </scm>
      <canRoam>true</canRoam>
      <disabled>false</disabled>
      <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
      <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
      <triggers/>
      <concurrentBuild>false</concurrentBuild>
      <builders/>
      <publishers>
        <hudson.plugins.ws__cleanup.WsCleanup plugin="ws-cleanup@0.37">
          <patterns class="empty-list"/>
          <deleteDirs>false</deleteDirs>
          <skipWhenFailed>false</skipWhenFailed>
          <cleanWhenSuccess>true</cleanWhenSuccess>
          <cleanWhenUnstable>true</cleanWhenUnstable>
          <cleanWhenFailure>true</cleanWhenFailure>
          <cleanWhenNotBuilt>true</cleanWhenNotBuilt>
          <cleanWhenAborted>true</cleanWhenAborted>
          <notFailBuild>false</notFailBuild>
          <cleanupMatrixParent>false</cleanupMatrixParent>
          <externalDelete></externalDelete>
          <disableDeferredWipeout>false</disableDeferredWipeout>
        </hudson.plugins.ws__cleanup.WsCleanup>
      </publishers>
      <buildWrappers/>
    </project>"""

def xml_file_contents = new XmlParser().parseText(xml_file)
println xml_file_contents.scm.locations[0].children()[0].remote.text()

输出:

代码语言:javascript
复制
http://LAPTOP/svn/localrepo/subfolder/componentname/RC
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59713983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档