python實現(xiàn)xml轉json文件的示例代碼
使用了Python的 xml.etree.ElementTree 庫
xml.etree.ElementTree 庫簡介
xml.etree.ElementTree模塊實現(xiàn)了一個簡單而高效的API用于解析和創(chuàng)建XML數(shù)據(jù)。xml.etree.ElementTree模塊對于惡意構造的數(shù)據(jù)是不安全的。如果您需要解析不受信任或未經驗證的數(shù)據(jù),請參閱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))
以上就是python實現(xiàn)xml轉json文件的示例代碼的詳細內容,更多關于python實現(xiàn)xml轉json文件的資料請關注腳本之家其它相關文章!
相關文章
詳解Python?Flask?API?示例演示(附cookies和session)
這篇文章主要為大家介紹了Python?Flask?API?示例演示(附cookies和session)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
如何利用Python讓Excel快速按條件篩選數(shù)據(jù)
平時總是要對Excel進行操作,整理了一下平時經常會用到的操作,下面這篇文章主要給大家介紹了關于如何利用Python讓Excel快速按條件篩選數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12
python numpy中對ndarry按照index增刪改查
這篇文章主要介紹了python numpy中對ndarry按照index增刪改查,在numpy中的ndarry是一個數(shù)組,因此index就是位置下標,注意下標是從0開始,接下來一起進入下面文章了解詳細內容吧2022-02-02

