-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmltools.py
71 lines (66 loc) · 2.14 KB
/
xmltools.py
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
67
68
69
70
#!/bin/python
import urllib
import os
import xml.etree.ElementTree as ET
#####################
# Helper functions
#####################
# Check for directory
def ensure_empty_dir(f):
if os.path.exists(f):
for fileName in os.listdir(f):
os.remove(f+"/"+fileName)
else:
os.makedirs(f)
# Nicely indents the XML output
def indent(elem, level=0):
i = "\n" + level*"\t"
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + "\t"
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
# Removes empty nodes from the tree
def removeemptytags(elem):
if elem.text:
elem.text = elem.text.strip()
toberemoved = []
for child in elem:
if len(child.attrib) != 0:
if 'errorplus' in child.attrib and (child.attrib['errorplus'] == None or child.attrib['errorplus'] == ""):
del child.attrib['errorplus']
if 'errorminus' in child.attrib and (child.attrib['errorminus'] == None or child.attrib['errorminus'] == ""):
del child.attrib['errorminus']
torem = False
try:
if child is None or (len(child) == 0 and len(child.text) == 0 and len(child.attrib) == 0):
torem = True
except:
torem = True
if torem:
toberemoved.append(child)
for child in toberemoved:
elem.remove(child)
for child in elem:
removeemptytags(child)
# Convert error to errorminus and errorplus
if 'ep' in elem.attrib:
err = elem.attrib['ep']
del elem.attrib['ep']
elem.attrib['errorplus'] = err
if 'em' in elem.attrib:
err = elem.attrib['em']
del elem.attrib['em']
elem.attrib['errorminus'] = err
if 'error' in elem.attrib:
err = elem.attrib['error']
del elem.attrib['error']
elem.attrib['errorminus'] = err
elem.attrib['errorplus'] = err