Python查找属性并删除父级和子级

Python查找属性并删除父级和子级,python,xml,xpath,Python,Xml,Xpath,我想使用查找属性来删除特定的xml元素 下面是我的xml文件示例 <domain type='kvm'> <name>ptest</name> <uuid>6119613e-4ba6-4be9-a1a2-9a33ca293341</uuid> <description>Ubuntu 12.04.3 LTS</description> <devices> <emulator

我想使用查找属性来删除特定的xml元素

下面是我的xml文件示例

<domain type='kvm'>
  <name>ptest</name>
  <uuid>6119613e-4ba6-4be9-a1a2-9a33ca293341</uuid>
  <description>Ubuntu 12.04.3 LTS</description>
  <devices>
    <emulator>/usr/bin/kvm-spice</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/ptest.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/home/iso/ubuntu-12.04.3-server-amd64.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/home/iso/freebsd-amd64.iso'/>
      <target dev='hdd' bus='ide'/>
      <readonly/>
    </disk>
  <devices>
</domain>

普泰斯特
6119613e-4ba6-4be9-a1a2-9a33ca293341
Ubuntu 12.04.3 LTS
/usr/bin/kvm spice
现在我想使用属性source='/home/iso/ubuntu-12.04.3-server-amd64.iso'删除磁盘块

你能帮我怎么做吗


Umar

假设您事先知道如何在python中使用XPath—因为我没有XPath—您可以尝试使用以下XPath来获取此问题中描述的
元素:

//disk[source/@file='/home/iso/freebsd-amd64.iso']

它基本上选择了所有的
元素,这些元素的子元素
属性
文件
值等于
'/home/iso/freebsd-amd64.iso'

您尝试了ElementTree吗?您尝试了什么?您使用什么框架/技术来遍历XML文档?它给了我一个错误(“预期的路径分隔符(%s)”%(op或tag)在google上的快速搜索提示我该错误特定于您使用的python库(ElementTree不支持属性选择器或其他什么,我真的不确定)。抱歉,无法帮助解决此问题。well其版本问题,我已签入python 2.7并在那里工作,无论如何,谢谢您的欢迎,您修复了错误,做得很好!别忘了接受答案。详细信息: