Skip to content
Advertisement

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.

Advertisement

Answer

All sub-elements (descendants) of elem:

JavaScript

A more complete example:

JavaScript

To exclude the root itself:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement