不能用元素树解析xml

时间:2018-05-01 17:46:37

标签: python xml python-3.x elementtree

我正在尝试为jenkins job xml做一个inlace更新。我有以下xml配置:

<?xml version='1.0' encoding='UTF-8'?>
<flow-definition plugin="workflow-job@2.10">
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty/>
      <hudson.model.ParametersDefinitionProperty>
      <parameterDefinitions>
        <hudson.model.ChoiceParameterDefinition>
          <name>environment</name>
          <description></description>
          <choices class="java.util.Arrays$ArrayList">
            <a class="string-array">
              <string>dev</string>
              <string>int</string>
              <string>stag</string>
              <string>prod</string>
            </a>
          </choices>
        </hudson.model.ChoiceParameterDefinition>
        <hudson.model.ChoiceParameterDefinition>
          <name>action</name>
          <description></description>
          <choices class="java.util.Arrays$ArrayList">
            <a class="string-array">
              <string>plan</string>
              <string>apply</string>
              <string>destroy</string>
            </a>
          </choices>
        </hudson.model.ChoiceParameterDefinition>
        <hudson.model.ChoiceParameterDefinition>
          <name>stack</name>
          <description></description>
          <choices class="java.util.Arrays$ArrayList">
            <a class="string-array">
              <string>blue</string>
              <string>green</string>
              <string>switch</string>
            </a>
          </choices>
        </hudson.model.ChoiceParameterDefinition>
        <hudson.model.ChoiceParameterDefinition>
          <name>region</name>
          <description></description>
          <choices class="java.util.Arrays$ArrayList">
            <a class="string-array">
              <string>virginia</string>
              <string>oregon</string>
            </a>
          </choices>
        </hudson.model.ChoiceParameterDefinition>
      </parameterDefinitions>
    </hudson.model.ParametersDefinitionProperty>
    <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
      <triggers/>
    </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
  </properties>
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="workflow-cps@2.26">
    <scm class="hudson.plugins.git.GitSCM" plugin="git@3.6.3">
      <configVersion>2</configVersion>
      <branches>
        <hudson.plugins.git.BranchSpec>
          <name>*/terraform9</name>
        </hudson.plugins.git.BranchSpec>
      </branches>
      <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
      <submoduleCfg class="list"/>
      <extensions/>
    </scm>
    <scriptPath>aws/awsJenkinsfile</scriptPath>
  </definition>
  <triggers/>
</flow-definition>

我需要解析并替换块:

<hudson.model.ChoiceParameterDefinition>
          <name>region</name>
          <description></description>
          <choices class="java.util.Arrays$ArrayList">
            <a class="string-array">
              <string>virginia</string>
              <string>oregon</string>
            </a>
          </choices>
        </hudson.model.ChoiceParameterDefinition>

我有以下代码:

    config = get_config()
    tree = ET.ElementTree(ET.fromstring(config))
    root = tree.getroot()

    for item in root.iter(tag="properties"):
        print(item.tag, item.attrib)

但是我没有获取属性的所有元素,而是在打印item.tag和attribute时得到 None

什么是在xml中添加更多<string>regions</string>以使用元素树进行xml更新的最pythonic方式?

0 个答案:

没有答案