python实现xml转json文件的示例代码
作者:撒欢 时间:2023-09-29 23:52:50
使用了Python的 xml.etree.ElementTree 库
xml.etree.ElementTree 库简介
xml.etree.ElementTree模块实现了一个简单而高效的API用于解析和创建XML数据。xml.etree.ElementTree模块对于恶意构造的数据是不安全的。如果您需要解析不受信任或未经验证的数据,请参阅XML漏洞。
参考文献:https://docs.python.org/3.6/library/xml.etree.elementtree.html
from xml.etree import ElementTree
import json
LISTTYPE = 1
DICTTYPE = 0
def getDictResults(res_dicts, iters):
result_dicts = {}
for iter in iters.getchildren():
iterxml(iter, result_dicts)
if result_dicts:
res_dicts[iters.tag].update(result_dicts)
def getListResults(res_dicts, iters):
result_lists = []
for iter in iters.getchildren():
result_dicts = {}
iterxml(iter, result_dicts)
result_lists.append(result_dicts.copy())
del(result_dicts)
if result_lists:
if len(res_dicts[iters.tag].items()) == 0:
res_dicts[iters.tag] = result_lists.copy()
else:
for resobj in result_lists:
resobjkey = list(resobj.keys())[0]
if res_dicts[iters.tag].get(resobjkey) == None:
res_dicts[iters.tag].update(resobj)
else:
if type(res_dicts[iters.tag][resobjkey]) == list:
res_dicts[iters.tag][resobjkey].append(resobj[resobjkey].copy())
else:
old_value = res_dicts[iters.tag][resobjkey]
res_dicts[iters.tag][resobjkey] = []
res_dicts[iters.tag][resobjkey].append(old_value)
res_dicts[iters.tag][resobjkey].append(resobj[resobjkey].copy())
del(result_lists)
def checkxmlchildrentype(iters):
taglist = []
for iter in iters.getchildren():
taglist.append(iter.tag)
if len(set(taglist)) == len(taglist):
return DICTTYPE
else:
return LISTTYPE
def getResults(res_dicts, iters):
if checkxmlchildrentype(iters):
return getListResults(res_dicts, iters)
else:
return getDictResults(res_dicts, iters)
#@res_dicts {}
def iterxml(iter, res_dicts):
res_dicts[iter.tag] = {}
if iter.attrib:
for k,v in dict(iter.attrib).items():
res_dicts[iter.tag].update({k : v})
if iter.text is not None and iter.text.strip() != "":
res_dicts[iter.tag].update({"__XmlTagText__" : iter.text.strip()})
if iter.getchildren():
getResults(res_dicts, iter)
def parserxmltojson(file_path):
try:
tree = ElementTree.parse(file_path)
except Exception as e:
#multi-byte encodings are not supported 把字符集改成utf-8就可以
#encoding specified in XML declaration is incorrect xml encoding标识和文件的字符集不同
#syntax error 语法错误,乱码等
#not well-formed (invalid token) 编辑器点击后字符集被修改成ASCII等,或者文件本身字符集和xml encoding不相同
print("Parser {} Error, Errmsg: {}".format(file_path, e))
return ""
if tree is None:
print("{} is None.".format(file_path))
return ""
root = tree.getroot()
report = {}
iterxml(root, report)
#return getDictResults(root)
return report
if __name__ == "__main__":
jsonret = parserxmltojson("test.xml")
with open("test.json", "w", encoding="utf-8") as fd:
fd.write(json.dumps(jsonret, ensure_ascii=False, indent=4))
print(json.dumps(jsonret, ensure_ascii=False, indent=4))
来源:https://www.cnblogs.com/frisk/p/12634427.html
标签:python,xml,json
0
投稿
猜你喜欢
解析一个通过添加本地分区索引提高SQL性能的案例
2023-07-22 13:29:32
用Dreamweaver设计限时自动关闭网页
2009-07-10 13:13:00
Python 解析库json及jsonpath pickle的实现
2021-09-10 00:27:14
Python Opencv图像处理基本操作代码详解
2023-06-08 08:08:05
JavaScript 数据结构之字典方法
2024-04-16 09:28:22
oracle复制表结构和复制表数据语句分享
2023-07-07 02:43:43
Python实现钉钉发送报警消息的方法
2022-11-18 06:06:44
PHP 中文处理技巧
2024-05-11 09:45:09
很好用的PHP数据库类
2024-05-11 09:52:10
python 判断一个进程是否存在
2021-06-21 02:19:24
编写python代码实现简单抽奖器
2023-04-07 12:32:48
Python进阶:生成器 懒人版本的迭代器详解
2021-12-21 16:08:05
MySQL主从搭建(多主一从)的实现思路与步骤
2024-01-18 01:14:02
Python中的Socket 与 ScoketServer 通信及遇到问题解决方法
2021-02-01 04:08:12
JavaScript中跨域调用Flash的方法
2024-04-19 10:25:49
详解用Python处理Args的3种方法
2023-01-21 17:10:10
Linux下安装Python3和django并配置mysql作为django默认服务器方法
2023-11-15 01:04:44
PyTorch 之 强大的 hub 模块和搭建神经网络进行气温预测
2022-11-11 14:41:15
python 等差数列末项计算方式
2022-01-29 10:12:57
python Pygame的具体使用讲解
2021-01-15 21:41:26