Find all nodes from XML using cElementTree

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

+1


source to share


2 answers


You can use XPath paths in findall method:

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

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

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



etree.findall('.//*')

      

+2


source


Have you looked at node.getiterator ()?



+1


source







All Articles