Python Forum
Adding a line number to an lxml Element
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a line number to an lxml Element
I am trying to find a way to add a line number from the original XML text to each element that is built in an XML tree from python's lxml modules. Something that would allow me to get an element from the tree and simply do something to the effect of:

line_num = element.xml_line_num
I know there is a sourceline property in lxml tree elements, but that is not reliable. It is not always correct. With the basic ElementTree provided by default with python 2.7, the default parser had a method called GetInputContext(), which I could use by looping through lines in the original XML text and comparing against the text gotten from that input context method. I quickly discovered CurrentLineNumber from the default parser is not reliable. Had to resort to XML source string comparisons.

Anyhow, I simply am not good enough with my understanding of subclassing parts of modules and getting things working. Matured and widely used Python modules tend to be very thorough and extensive in its use of so many aspects of Python, and when you get down to the point that you are dealing with c-code implementations of the underlying base classes it gets confusing. So, some help, and maybe some example code, would be really appreciated. Thanks in advance!

Let me add some clarification. I would like to have code that looks like this:

from lxml import etree as ET

xml_tree = ET.fromstringlist(xml_file_lines)

for xml_element in xml_tree.iter():

    line_num = xml_element.xml_line_num
'xml_file_lines' is a list of the XML file lines read in from a file. Some of which are empty lines. What I can say about the XML lines in the files I am processing is that non-whitespace lines and lines that aren't comments do show as either opening, closing, self-closing, or open/closing elements. That is to say like this:

<tag1 />
<tag2 id = "yum" />
<tag2 id = "delicious" name = "tasty">text</tag>

There will never be more than one opening or one closing XML tag on any single line in these XML files. Any ideas?

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding an ascending number AlphaInc 3 228 Jul-11-2021, 10:13 AM
Last Post: perfringo
  Adding List Element if Second part of the List Elements are the Same quest_ 3 617 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Counting Number of Element in a smart way quest 2 480 Nov-09-2020, 10:24 PM
Last Post: quest
  adding elements to a list that are more than a specific number Olavv 2 807 Mar-19-2020, 06:05 PM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 971 Mar-08-2020, 11:16 AM
Last Post: ndc85430
  line number of first and second occurance of string in a file mdalireza 1 682 Nov-18-2019, 09:55 AM
Last Post: perfringo
  Search for the line number corresponding to a value Lali 0 584 Oct-22-2019, 08:56 AM
Last Post: Lali
  Adding markers to Folium map only adding last element. tantony 0 842 Oct-16-2019, 03:28 PM
Last Post: tantony
  naming images adding to number within multiple functions Bmart6969 0 737 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  How to list number of times element is in defaultdict and delete it mrapple2020 3 1,080 Apr-15-2019, 07:34 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020