Last active
April 3, 2020 15:29
-
-
Save gholker/8cc3796dac8a741f6249841206194afd to your computer and use it in GitHub Desktop.
Parse and print XML to verify the structure manually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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