Skip to content

Instantly share code, notes, and snippets.

@plq
Last active August 29, 2015 14:01
Show Gist options
  • Save plq/025005a71e8135c46800 to your computer and use it in GitHub Desktop.
Save plq/025005a71e8135c46800 to your computer and use it in GitHub Desktop.
from collections import deque
from io import BytesIO
from lxml import etree
istr = BytesIO(b"""<object class="EnumDnSched">
<field name="enumDn">
<value>343741014</value>
</field>
<field name="naptrFlags">
<value>nu</value>
</field>
</object>
<object class="EnumDnSched">
<field name="enumDn">
<value>343741015</value>
</field>
<field name="naptrFlags">
<value>nu</value>
</field>
</object>""")
events = etree.iterparse(istr, events=("start", "end"))
stack = deque()
for event, element in events:
if event == "start":
stack.append(element)
elif event == "end":
stack.pop()
if len(stack) == 0:
break
print(istr.tell(), "%5s, %4s, %s" % (event, element.tag, element.text))
try:
next(events)
except Exception as e:
print(e.args)
print(e.offset)
327 start, object,
327 start, field,
327 start, value, 343741014
327 end, value, 343741014
327 end, field,
327 start, field,
327 start, value, nu
327 end, value, nu
327 end, field,
('Extra content at the end of the document, line 9, column 1',)
None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment