Skip to content

Instantly share code, notes, and snippets.

@strets123
Created November 10, 2017 23:50
Show Gist options
  • Save strets123/8aa8eca421971ddda4d6a2762accd5a3 to your computer and use it in GitHub Desktop.
Save strets123/8aa8eca421971ddda4d6a2762accd5a3 to your computer and use it in GitHub Desktop.
import xml.etree.ElementTree as ET
import sys
# Main
def process(stream, prefix) :
items = []
# Parse the XML
tree = ET.parse(stream)
start = 'TradeMark'
# Get root element
data = None
for elem in tree.iter():
print(elem.tag)
if elem.tag.strip() == start:
if data:
items.append(dict(data))
data = {}
if data is not None and elem.text:
t = elem.text.strip()
if not elem.tag in data:
data[elem.tag] = t
else:
data[elem.tag] = [data[elem.tag]] + [t]
print(items[0])
# Linearize
# Each argument is a file
args = sys.argv[1:]
# Loop on files
for filename in args :
# Open the file
file = open(filename)
# If we process several files, prefix each one with its path
if len(args) > 1 :
prefix = filename + ":"
else:
prefix = ""
# Process it
process(file, prefix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment