ElementTree 删除元素

use*_*192 2 python xml scripting elementtree python-2.7

这里是 Python 菜鸟。想知道什么是对的干净,最好的办法删除所有“ profile”用标签updated的属性值true

我已经尝试了以下代码,但它正在抛出:SyntaxError("cannot use absolute path on element")

 root.remove(root.findall("//Profile[@updated='true']"))
Run Code Online (Sandbox Code Playgroud)

XML:

<parent>
  <child type="First">
    <profile updated="true">
       <other> </other>
    </profile>
  </child>
  <child type="Second">
    <profile updated="true">
       <other> </other>
    </profile>
  </child>
  <child type="Third">
     <profile>
       <other> </other>
    </profile>
  </child>
</parent>
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 6

如果使用xml.etree.ElementTree,则应使用remove()方法删除节点,但这需要您拥有父节点引用。因此,解决方案:

import xml.etree.ElementTree as ET

data = """
<parent>
  <child type="First">
    <profile updated="true">
       <other> </other>
    </profile>
  </child>
  <child type="Second">
    <profile updated="true">
       <other> </other>
    </profile>
  </child>
  <child type="Third">
     <profile>
       <other> </other>
    </profile>
  </child>
</parent>"""

root = ET.fromstring(data)
for child in root.findall("child"):
    for profile in child.findall(".//profile[@updated='true']"):
        child.remove(profile)

print(ET.tostring(root))
Run Code Online (Sandbox Code Playgroud)

印刷:

<parent>
  <child type="First">
    </child>
  <child type="Second">
    </child>
  <child type="Third">
     <profile>
       <other> </other>
    </profile>
  </child>
</parent>
Run Code Online (Sandbox Code Playgroud)

请注意,lxml.etree这样会更简单一些:

root = ET.fromstring(data)
for profile in root.xpath(".//child/profile[@updated='true']"):
    profile.getparent().remove(profile)
Run Code Online (Sandbox Code Playgroud)

在哪里ET

import lxml.etree as ET
Run Code Online (Sandbox Code Playgroud)