arthur.johnson221
arthur.johnson221

Reputation: 53

Get an element of a tree by path

I have some XML file. I know that the element I need is located in some path \element1\element2\element3. How do I access it? I only found a way to access an element if it's a child of a root:

import xml.etree.ElementTree as ET

root = ET.parse('my_xml.xml').getroot()

for child in root.findall('element1'):
    output = child.text

Also, I can't just search for element3 because this name appears in different parts of the xml.

Upvotes: 1

Views: 389

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54970

Did you look at the documentation for findall?

output = root.findall( './element1/element2/element3' )

Upvotes: 1

Related Questions