While parsing an XML document with lxml I want to find the starting and ending line numbers of a particular tag. I am able to find the starting tag's position by using the sourceline
property on lxml.etree.Element
, however I am struggling at finding the closing tag's line number.
A trivial example of my attempt:
import lxml.etree as ET
xml_sample = b'''<?xml version="1.0" encoding="utf-8"?>
<collection>
<item>
<value>foo</value>
</item>
<item>
<value>
bar
</value>
</item>
</collection>'''
for el in ET.fromstring(xml_sample).getroottree().findall('//value'):
print('Found value "{el.text}" starting on line {el.sourceline} '
'and ending on line ???.'.format(el=el))
Is it possible to get the closing tag line numbers of the value
elements in the above example?