I am working with a huge xml-file and try to extract information from different elements.
import xml.etree.ElementTree as ET
tree = ET.parse('t.xml')
root = tree.getroot()
To find the elements I use the find method:
elm = root.find('.//Element[@elmid="1234"]')
From this I extract information and in addition I need information from the parent element. But elm.find('..')
returns only None
as documented here:
https://docs.python.org/3/library/xml.etree.elementtree.html
Now I use the folowing:
prt = root.find('.//Element[@elmid="1234"]/..')
elm = prt.find('/Element[@elmid="1234"]')
This looks a bit unnatural to me, but works.
Do you know a better way to do this?
Do you know why only None
is returned?