Skip to content

Instantly share code, notes, and snippets.

@gholker
Last active April 3, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gholker/8cc3796dac8a741f6249841206194afd to your computer and use it in GitHub Desktop.
Save gholker/8cc3796dac8a741f6249841206194afd to your computer and use it in GitHub Desktop.
Parse and print XML to verify the structure manually
import os
import sys
from lxml import etree as ET
def recurse(node, level=0):
for child in node.iterchildren():
text = (child.text or '')
if len(text) > 20:
text = text[:20] + '...'
to_print = child.tag + ': ' + text.replace('\n', '\\n')
print(' ' * (level * 2) + to_print)
recurse(child, level + 1)
if __name__ == '__main__':
path = os.path.expanduser(sys.argv[1])
huge_tree_parser = ET.XMLParser(huge_tree=True)
tree = ET.parse(path, huge_tree_parser)
root = tree.getroot()
recurse(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment