Search code examples
pythonxmllxml

Get all nodes of particular name in lxml?


I'm finding the initial learning curve a bit steep with lxml - just common tasks such as grabbing nodes by name, attribute, and get their content. Here's 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 be using objectify, xpath...

Comments also welcome on the way that I'm reading in the file and turning it into a parsable lxml object. Thanks.


Solution

  • See the documentation:

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