Jenkinsfile XmlParser()。parseText(xml_file)不适用于XML节点名称

问题描述 投票:0回答:1

需要从某些XML文件中提取一些值。从Jenkins文件触发提取。以下代码在我的Jenkinsfile中可以正常工作:

    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()方法进行遍历将是一个更好的主意。但是,每次我应用这样的内容:

    def value_i_need_to_extract = xml_file_contents.nodename.nodename.nodename.nodename.text().toString()

我收到以下错误:

    org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote

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

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

说明问题的示例:

代码A:

def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].children()[0]
echo 'Remote repository is:' + remote_repo.toString()

输出A:

Remote repository is:remote[attributes={}; value=[http://LAPTOP/svn/localrepo/app/component/RC]]

代码B:

def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].children()[0].text()
echo 'Remote repository is:' + remote_repo.toString()

输出B:

Remote repository is:http://LAPTOP/svn/localrepo/app/component/RC

代码C:

def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].remote.text()

输出C:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote

您知道这种行为的可能原因吗?有没有一种方法可以通过节点名称遍历XML?

事先加点格拉西亚斯。

示例XML文件:

    <?xml version='1.1' encoding='UTF-8'?>
    <project>
      <description>Just a sample build job</description>
      <keepDependencies>false</keepDependencies>
      <properties>
        <hudson.plugins.jira.JiraProjectProperty plugin="[email protected]"/>
      </properties>
      <scm class="hudson.scm.SubversionSCM" plugin="[email protected]">
        <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="[email protected]">
          <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>
xml jenkins jenkins-pipeline devops traversal
1个回答
0
投票

更新。

正在申请:

new XmlParser().parse(xml_file)

而不是:

new XmlParser().parseText(xml_file)

产生错误:

java.net.MalformedURLException: no protocol: 
© www.soinside.com 2019 - 2024. All rights reserved.