Python etree-查找精确匹配

Python etree-查找精确匹配,python,xml,python-3.5,elementtree,xml.etree,Python,Xml,Python 3.5,Elementtree,Xml.etree,我有以下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="D

我有以下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>
但在这种情况下,我必须知道该条目在树中的确切位置,所以它不灵活,我不知道如何更改它

我也试过这样的方法:

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')
但我不知道如何将这一点添加到我的示例中。 我不想在这个案例中使用正则表达式。
非常感谢您的帮助。

您可以使用找到特定的
条目

结果(output.xml):


描述

您可以使用找到特定的
条目
元素

结果(output.xml):


描述

这正是我想要的,现在看起来很简单。。。当我尝试
打印(entry)
时,我得到了
-这可能得到“正常值”吗?也许你想要的是
打印(entry.tag)
entry.text
将是空的,因为元素没有内容。现在,我注意到,xml定义和!缺少DOCTYPE。我处理过xml声明,但无法处理!DOCTYPE。有没有办法保存它?也许这对你有用:。如果没有帮助,我建议你发布一个新问题。这当然是我想要的,现在看起来很简单。。。当我尝试
打印(entry)
时,我得到了
-这可能得到“正常值”吗?也许你想要的是
打印(entry.tag)
entry.text
将是空的,因为元素没有内容。现在,我注意到,xml定义和!缺少DOCTYPE。我处理过xml声明,但无法处理!DOCTYPE。有没有办法保存它?也许这对你有用:。如果没有帮助,我建议你发布一个新问题。
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')
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")
<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>