40

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.

maciejwww
  • 1,067
  • 1
  • 13
  • 26
j5shi
  • 797
  • 1
  • 8
  • 21

5 Answers5

28

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']
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 2
    Sorry Eli, but maybe I didn't make myself understood, I just want to get all the sub-elements, not also the root. i.e. the root is unwanted here. but I think your method also contains the root object, right? – j5shi May 02 '12 at 07:04
  • 2
    But what if there are more than one sturct with tag 'a' nested in element 'a' and I want to get all sub-elements of all 'a' structs? – j5shi May 02 '12 at 07:17
  • 1
    The element objects are iterable also without using the `iter()`. The element behaves also like a list; so, you can also index the subelements. – pepr May 02 '12 at 07:20
  • 2
    @pepr: yes, but that only gives you the element's immediate children, not all descendants – Eli Bendersky May 02 '12 at 07:23
  • 2
    @Eli Bendersky: I see. But the `getchildren()` also returns only the immediate children. The old equivalent of the new `list(elem.iter())` is the `list(elem.getiterator())`. It depends what Steven really wants. – pepr May 02 '12 at 11:24
18

in the pydoc it is mentioned to use list() method over the node to get child elements.
list(elem)

Harshal Zope
  • 1,458
  • 1
  • 11
  • 12
8

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.

pepr
  • 20,112
  • 15
  • 76
  • 139
3

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')
Turtles Are Cute
  • 3,200
  • 6
  • 30
  • 38
3

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 e.g. '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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Eypros
  • 5,370
  • 6
  • 42
  • 75