Mohit Ranka
Mohit Ranka

Reputation: 4271

Find all nodes from an XML using cElementTree

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

Upvotes: 2

Views: 1544

Answers (2)

S.Lott
S.Lott

Reputation: 392010

Have you looked at node.getiterator()?

Upvotes: 1

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340476

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('.//*')

Upvotes: 3

Related Questions