forked from knadh/xmlutils.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2json.py
84 lines (66 loc) · 1.78 KB
/
xml2json.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
xml2json.py
Kailash Nadh, http://nadh.in
December 2012
License: MIT License
Documentation: http://nadh.in/code/xmlutils.py
"""
import codecs
import xml.etree.ElementTree as et
import json
def elem2list(elem):
"""
Convert an ElementTree element to a list
"""
block = {}
# get the element's children
children = elem.getchildren()
if children:
cur = map(elem2list, children)
# create meaningful lists
scalar = False
try:
if elem[0].tag != elem[1].tag: # [{a: 1}, {b: 2}, {c: 3}] => {a: 1, b: 2, c: 3}
cur = dict(zip(
map(lambda e: e.keys()[0], cur),
map(lambda e: e.values()[0], cur)
))
else:
scalar = True
except Exception as e: # [{a: 1}, {a: 2}, {a: 3}] => {a: [1, 2, 3]}
scalar = True
if scalar:
if len(cur) > 1:
cur = {elem[0].tag: [e.values()[0] for e in cur if e.values()[0] is not None]}
else:
cur = {elem[0].tag: cur[0].values()[0] }
block[elem.tag] = cur
else:
val = None
if elem.text:
val = elem.text.strip()
val = val if len(val) > 0 else None
block[elem.tag] = val
return block
def xml2json(elem, pretty=True):
"""
Convert an ElementTree Element (root) to json
"""
# if the given Element is not the root element, find it
if hasattr(elem, 'getroot'):
elem = elem.getroot()
return json.dumps(elem2list(elem), indent=(4 if pretty else None))
def xml2json_file(input_file, output=None, pretty=True, encoding='utf-8'):
"""
Convert xml file to json
"""
context = et.iterparse(input_file, events=("start", "end"))
context = iter(context)
event, root = context.next()
json = xml2json(root, pretty)
# if an output filename is given, write to it, otherwise, return json
if output is not None:
output = codecs.open(output, "w", encoding)
output.write(json)
else:
return json