-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
xml_common.py
59 lines (53 loc) · 1.77 KB
/
xml_common.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
#!/usr/bin/env python
from __future__ import print_function
#import xml.etree.ElementTree as ET
import lxml.etree as ET
#try:
# from lxml import etree
# print("running with lxml.etree")
#except ImportError:
# try:
# # Python 2.5
# import xml.etree.cElementTree as etree
# print("running with cElementTree on Python 2.5+")
# except ImportError:
# try:
# # Python 2.5
# import xml.etree.ElementTree as etree
# print("running with ElementTree on Python 2.5+")
# except ImportError:
# try:
# # normal cElementTree install
# import cElementTree as etree
# print("running with cElementTree")
# except ImportError:
# try:
# # normal ElementTree install
# import elementtree.ElementTree as etree
# print("running with ElementTree")
# except ImportError:
# print("Failed to import ElementTree from any known place")
def indent(elem, level=0, more_sibs=False):
i = "\n"
num_kids = len(elem)
if level:
i += (level-1) * ' '
#print(level, elem.tag, num_kids, more_sibs)
if num_kids:
if not elem.text or not elem.text.strip():
elem.text = i + " "
if level:
elem.text += ' '
count = 0
for kid in elem:
indent(kid, level+1, count < num_kids - 1)
count += 1
if not elem.tail or not elem.tail.strip():
elem.tail = i
if more_sibs:
elem.tail += ' '
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
if more_sibs:
elem.tail += ' '