-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict2xml.py
69 lines (61 loc) · 2.12 KB
/
dict2xml.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
# -*- coding: utf-8 -*-
from xml.dom.minidom import parseString
__author__ = 'snail'
__date__ = '2018/7/13'
attr_symbol = ('@', '-')
text_symbol = ('#text',)
def recursion(obj):
"""
递归进行dict到xml转换
:return: xml:一个节点内部xml, attrs:一个节点的属性
"""
if not isinstance(obj, dict):
raise RuntimeError("请传入dict对象")
xml = ''
attrs = []
for key, value in obj.items():
if len(key) > 0 and key[0:1] in attr_symbol:
attrs.append(' ' + key[1:len(key)] + '=' + '"' + value + '"')
elif key in text_symbol:
xml = value
else:
child_attrs = []
child_xml = ''
is_list = False
if isinstance(value, dict):
# 子节点为dict
child_xml, child_attrs = recursion(value)
elif isinstance(value, list):
# 子节点为list
is_list = True
for item in value:
# 遍历列表
if isinstance(item, dict):
child_xml, child_attrs = recursion(item)
xml += '<' + key
for child_attr in child_attrs:
xml += child_attr
xml += '>'
# add child
xml += child_xml
xml += '</' + key + '>'
else:
xml += '<' + key + '>' + item + '</' + key + '>'
else:
# 子节点为text
child_xml = value
if not is_list:
# 如果不是列表则
xml += '<' + key
for child_attr in child_attrs:
xml += child_attr
xml += '>'
# add child
xml += child_xml
xml += '</' + key + '>'
return xml, attrs
def dict2xml(obj):
result = '<?xml version="1.0" encoding="utf-8"?>'
result += recursion(obj)[0] # 递归生成
result = parseString(result).toprettyxml()
return result