python xml转json
class Xml2Json:
LIST_TAGS = ['COMMANDS']
def __init__(self, data=None):
self._parser = ParserCreate()
self._parser.StartElementHandler = self.start
self._parser.EndElementHandler = self.end
self._parser.CharacterDataHandler = self.data
self.result = None
if data:
self.feed(data)
self.close()
def feed(self, data):
self._stack = []
self._data = ''
self._parser.Parse(data, 0)
def close(self):
self._parser.Parse("", 1)
del self._parser
def start(self, tag, attrs):
assert attrs == {}
assert self._data.strip() == ''
self._stack.append([tag])
self._data = ''
def end(self, tag):
last_tag = self._stack.pop()
assert last_tag[0] == tag
if len(last_tag) == 1: # leaf
data = self._data
else:
if tag not in Xml2Json.LIST_TAGS:
# build a dict, repeating pairs get pushed into lists
data = {}
for k, v in last_tag[1:]:
if k not in data:
data[k] = v
else:
el = data[k]
if type(el) is not list:
data[k] = [el, v]
else:
el.append(v)
else: # force into a list
data = [{k: v} for k, v in last_tag[1:]]
if self._stack:
self._stack[-1].append((tag, data))
else:
self.result = {tag: data}
self._data = ''
def data(self, data):
self._data = data
python 字典转XML
def trans_dict_to_xml(data_dict):
"""
定义字典转XML的函数
:param data_dict:
:return:
"""
data_xml = []
for k in sorted(data_dict.keys()): # 遍历字典排序后的key
v = data_dict.get(k) # 取出字典中key对应的value
if k == 'detail' and not v.startswith('<![CDATA['): # 添加XML标记
v = '<![CDATA[{}]]>'.format(v)
data_xml.append('<{key}>{value}</{key}>'.format(key=k, value=v))
return '<xml>{}</xml>'.format(''.join(data_xml)) # 返回XML