【问题标题】:Python etree - find exact matchPython etree - 找到完全匹配
【发布时间】:2017-01-25 20:04:36
【问题描述】:

我有以下 xml 文件:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE TaskDefinition PUBLIC "xxx" "yyy">
<TaskDefinition created="time_stamp" formPath="path/sometask.xhtml" id="sample_id" modified="timestamp_b" name="sample_task" resultAction="Delete" subType="subtype_sample_task" type="sample_type">
  <Attributes>
    <Map>
      <entry key="applications" value="APP_NAME"/>
      <entry key="aaa" value="true"/>
      <entry key="bbb" value="true"/>
      <entry key="ccc" value="true"/>
      <entry key="ddd" value="true"/>
      <entry key="eee" value="Disabled"/>
      <entry key="fff"/>
      <entry key="ggg"/>
    </Map>
  </Attributes>
  <Description>Description.</Description>
  <Owner>
    <Reference class="sample_owner_class" id="sample_owner_id" name="sample__owner_name"/>
  </Owner>
  <Parent>
    <Reference class="sample_parent_class" id="sample_parent_id" name="sample_parent_name"/>
  </Parent>
</TaskDefinition>

我想搜索: &lt;entry key="applications" value="APP_NAME"/&gt;

并将 value 更改为 ie.: `APP_NAME_2。

我知道我可以通过这个提取这个值:

import xml.etree.cElementTree as ET

tree = ET.ElementTree(file='sample.xml')
root = tree.getroot()

print(root[0][0][0].tag, root[0][0][0].attrib)

但在这种情况下,我必须知道条目在树中的确切位置 - 所以它不灵活,我不知道如何更改它。

也试过这样的:

for app in root.attrib:
    if 'applications' in root.attrib:
        print(app)

但我想不通,为什么这什么都不返回。

在 python 文档中,有以下示例:

for rank in root.iter('rank'):
    new_rank = int(rank.text) + 1
    rank.text = str(new_rank)
    rank.set('updated', 'yes')    
tree.write('output.xml')

但我不知道如何将此添加到我的示例中。 我不想在这种情况下使用正则表达式。 任何帮助表示赞赏。

【问题讨论】:

    标签: python xml python-3.5 elementtree xml.etree


    【解决方案1】:

    您可以使用XPath 定位特定的entry 元素。

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("sample.xml")   
    
    # Find the element that has a 'key' attribute with a value of 'applications'
    entry = tree.find(".//entry[@key='applications']")
    
    # Change the value of the 'value' attribute
    entry.set("value", "APP_NAME_2")
    
    tree.write("output.xml")
    

    结果(输出.xml):

    <TaskDefinition created="time_stamp" formPath="path/sometask.xhtml" id="sample_id" modified="timestamp_b" name="sample_task" resultAction="Delete" subType="subtype_sample_task" type="sample_type">
      <Attributes>
        <Map>
          <entry key="applications" value="APP_NAME_2" />
          <entry key="aaa" value="true"/>
          <entry key="bbb" value="true"/>
          <entry key="ccc" value="true"/>
          <entry key="ddd" value="true"/>
          <entry key="eee" value="Disabled"/>
          <entry key="fff"/>
          <entry key="ggg"/>
        </Map>
      </Attributes>
      <Description>Description.</Description>
      <Owner>
        <Reference class="sample_owner_class" id="sample_owner_id" name="sample__owner_name"/>
      </Owner>
      <Parent>
        <Reference class="sample_parent_class" id="sample_parent_id" name="sample_parent_name"/>
      </Parent>
    </TaskDefinition>
    

    【讨论】:

    • 这正是我想要的,现在看起来很简单......当我尝试print(entry)时,我得到&lt;Element 'entry' at 0x00000000015C3E08&gt; - 是否有可能获得“正常值”?
    • 也许你想要的是print(entry.tag)
    • entry.text 将为空,因为该元素没有内容。
    • 现在,我注意到缺少 xml 定义和 !DOCTYPE。我处理了 xml 声明,但无法使用 !DOCTYPE。有什么办法保存吗?
    • 也许这对你有用:stackoverflow.com/a/8868551/407651。如果没有帮助,我建议您发布一个新问题。
    猜你喜欢
    • 2014-05-08
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多