-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxml_tree_unifier.py
More file actions
66 lines (49 loc) · 2.04 KB
/
xml_tree_unifier.py
File metadata and controls
66 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# from an xml file create a mathematical set union of all children, attributes and values in an overly complicated way
import xml.etree.ElementTree as ET
import os
def find_all(name, path): # for looking through directory for filename
result = []
for root, dirs, files in os.walk(path):
if name in files:
result.append(os.path.join(root, name))
return result
def att_iter(elem, elem_dict): # iterates over all atttributes from input element
if elem.attrib:
for att in elem.attrib:
if att not in elem_dict:
elem_dict[att] = [elem.get(att)]
elif elem.get(att) not in elem_dict[att]:
elem_dict[att].append(elem.get(att))
def child_iter(elem, elem_dict): # iterates over all children from input element
att_iter(elem, elem_dict[0])
if list(elem):
for child in elem:
if child.tag not in elem_dict[1]:
elem_dict[1][child.tag] = [{}, {}]
print(elem.tag, '>>', child.tag)
child_iter(child,elem_dict[1][child.tag])
def elem_write(elem, scope, file): # writes out nested dictionary created by child_iter()
if type(elem) is list:
if all(isinstance(child, (list, dict)) for child in elem):
for num in range(len(elem)):
elem_write(elem[num], scope, file)
else:
file.write(' : ["'+'", "'.join(elem)+'"]')
elif type(elem) is dict:
for child in elem:
file.write('\n'+' '*scope + child)
elem_write(elem[child], scope + 1, file)
def update_union():
unit = [{}, {}]
for path in find_all('UnitData.xml', 'data'):
root = ET.parse(path).getroot()
child_iter(root, unit)
with open('Unified UnitData.txt','w') as f:
elem_write(unit, 0, f)
button = [{}, {}]
for path in find_all('ButtonData.xml', 'data'):
root = ET.parse(path).getroot()
child_iter(root, button)
with open('Unified ButtonData.txt','w') as f:
elem_write(button, 0, f)
update_union()