Reputation: 127
Are there any XML parsing libraries in Python that track the line number of each element? I am writing a script to verify XML settings, and it would be useful to print line numbers if my script detects an invalid line.
Upvotes: 2
Views: 802
Reputation: 386230
lxml can be used to parse xml and retain line numbers. Here is a simple example:
from lxml import etree
xml = '''
<foo>
<bar baz="1">hello</bar>
<bar baz='2'>world</bar>
</foo>
'''
root = etree.fromstring(xml)
for bar in root.findall("bar"):
baz = bar.attrib.get('baz', None)
if int(baz) > 1:
raise Exception("baz must not be greater than 1 on line %s" % bar.sourceline)
Upvotes: 3
Reputation: 474031
I think what you really need to do is to validate the XML file against an expected XML schema. lxml
can do that for you. The validation log would contain all the information you need to locate the problem in the XML file.
Upvotes: 2