2

Is there a way to find all nodes in a xml tree using cElementTree? The findall method works only for specified tags.

Mohit Ranka
  • 4,193
  • 12
  • 41
  • 41

2 Answers2

3

You can use XPath paths on the findall method:

The 1.2 release supports simple element location paths. In its simplest form, a location path is one or more tag names, separated by slashes (/).

You can also use an asterisk (*) instead of a tag name, to match all elements at that level. For example, */subtag returns all subtag grandchildren.

An empty tag (//) is used to search on all levels of the tree, beneath the current level. The empty tag must always be followed by a tag name or an asterisk.

etree.findall('.//*')
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
1

Have you looked at node.getiterator()?

S.Lott
  • 384,516
  • 81
  • 508
  • 779