简体   繁体   中英

Remove element and children from XML with elementtree Python

Tried a few different libraries now and think im close but cant figure out this problem.

I have an XML file with some nested tables which I want to remove. These are several levels down the XML hierarchy.

So far I have tried this...

import xml.etree.ElementTree as ET
import os

tree = ET.parse('/Users/me/file.xml')
root = tree.getroot()

for sect1 in root.findall('section1'):
    for sect2 in sect1.iter() :
        if sect2.tag == 'table':
            sect1.remove(sect2)

However I get the error:

ValueError: list.remove(x): x not in list

I can successfully remove sections of the document from the top level of the hierarchy using the following code:

import xml.etree.ElementTree as ET
import os

tree = ET.parse('/Users/me/file.xml')
root = tree.getroot()

for sect1 in root.findall('section1'):
    root.remove(sect1)

Im just missing how to remove elements which are further down from the top level.

Any help greatly appreciated.

use this:

for sect1 in root.findall('.//section1'):
root.remove(sect1)

the .// selects from all child section1 elements from the first element. you can be more specific selecting elements with './section1/section2' also selecting elements with particular attributes is possible with ./section1[@Name="SomeValueForNameAttribute"]' if you'd like to know more this is called xpath and the boiled down version that element tree offers is documented here

I use minidom to parse xml files and strings and it's so easy with minidom to do anything you want, and this is an example which you requested but using xml.dom.minidom library:-

from xml.dom.minidom import parse

doc = parse('/Users/me/file.xml')
root = doc.documentElement

for parent in root.childNodes:
    for child in parent.childNodes:
        if(child.tagName == 'table'):
            parent.removeChild(child)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM