Get all nodes of a specific name in lxml?

I find the initial learning curve a little steep with lxml - just common tasks, such as grabbing nodes by name, attribute and getting their contents. Here is a very simple question.

I have an XML file. I would like to find all the XML nodes called <Review>. How can I do this most efficiently with lxml?

f = open('./test.xml')
xml = f.read()
tree = etree.parse(StringIO(xml))
context = etree.iterparse(StringIO(xml))
# How to get all the tags with the name <Review>
reviews = tree.findall('Review') # Something like this?

I don't know if I should use objectify, xpath ...

Comments are also welcome along the path that I read in the file, and turning it into an parsed lxml object. Thank you

+3
source share
1 answer

See the documentation:

tree = etree.parse(open('./test.xml'))
reviews = tree.findall(".//Review")
+5
source

All Articles