You could use the iterparse
method, which is meant for handling large xml files. However, your file has an especially simple structure. Using iterparse would be unnecessarily complicated.
I will provide two answers in one script. I answer your question directly by showing how to parse lines in the xml using lxml and I provide what I think is likely to be a better answer using a regex.
The code reads each line in the xml and ignores those lines that do not begin with 'try ... except. When the script finds such a line it passes it to etree
from lxml for parsing then displays the attributes from the line. Afterwards it uses a regex to parse out the same attributes and to display them.
I strongly suspect that the regex would be faster.
>>> from lxml import etree
>>> report = '''\
... <report>
... <table>
... <detail name="John" surname="Smith">
... <detail name="Michael" surname="Smith">
... <detail name="Nick" surname="Smith">
... </table>
... </report>'''
>>> import re
>>> re.search(r'name="([^"]*)"\s+surname="([^"]*)', line).groups()
('John', 'Smith')
>>> for line in report.split('\n'):
... if line.strip().startswith('<detail'):
... tree = etree.fromstring(line.replace('>', '/>'))
... tree.attrib['name'], tree.attrib['surname']
... re.search(r'name="([^"]*)"\s+surname="([^"]*)', line).groups()
...
('John', 'Smith')
('John', 'Smith')
('Michael', 'Smith')
('Michael', 'Smith')
('Nick', 'Smith')
('Nick', 'Smith')