简体   繁体   中英

How to get all sub-elements of an element tree with Python ElementTree?

I want to find a way to get all the sub-elements of an element tree like the way ElementTree.getchildren() does, since getchildren() is deprecated since Python version 2.7.
I don't want to use it anymore, though I can still use it currently.

All sub-elements (descendants) of elem :

all_descendants = list(elem.iter())

A more complete example:

>>> import xml.etree.ElementTree as ET
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> c = ET.SubElement(a, 'c')
>>> d = ET.SubElement(a, 'd')
>>> e = ET.SubElement(b, 'e')
>>> f = ET.SubElement(d, 'f')
>>> g = ET.SubElement(d, 'g')
>>> [elem.tag for elem in a.iter()]
['a', 'b', 'e', 'c', 'd', 'f', 'g']

To exclude the root itself:

>>> [elem.tag for elem in a.iter() if elem is not a]
['b', 'e', 'c', 'd', 'f', 'g']

在 pydoc 中提到在节点上使用 list() 方法来获取子元素。
list(elem)

If you want to get all elements 'a', you can use:

a_lst = list(elem.iter('a'))

If the elem is also 'a', it will be included.

Maybe this does not correspond to OP actual question but in a greater sense I would suggest that if someone want to get all elements named with a certain name eg 'object' can use (an alternative approach to @Turtles Are Cute which to me at least seems more natural):

objs = tree.findall('object')

which also returns a list.

None of the existing answers will find all children. This solution uses BeautifulSoup instead of ETree, but will find all children, instead of just top-level:

from bs4 import BeautifulSoup    

with open(filename) as f:
    soup = BeautifulSoup(f, 'xml')

results = soup.find_all('element_name')

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