I would like to read quite big XML as a stream. But could not find any way to use my old XPathes to find elements. Previously files were of moderate size, so in was enough to:
all_elements = []
for xpath in list_of_xpathes:
all_elements.append(etree.parse(file).getroot().findall(xpath))
Now I am struggling with iterparse. Ideally the solution would be to compare path of current element with desired xpath:
import lxml.etree as et
xml_file = r"my.xml" # quite big xml, that i should read
xml_paths = ['/some/arbitrary/xpath', '/another/xpath']
all_elements = []
iter = et.iterparse(xml_file, events = ('end',))
for event, element in iter:
for xpath in xml_paths:
if element_complies_with_xpath(element, xpath):
all_elements.append(element)
break
How is it possible to implement element_complies_with_xpath function using lxml?