xml格式轉成json格式,使用Python


 

import xml.etree.ElementTree
root=xml.etree.ElementTree.parse('testXml.xml')
book=root.findall('person')
for book_list in book:
    for note in book_list:
        print(note.tag+':'+note.text)

 


f = open('testJson.json','a',encoding="utf-8")
for each in root1.getiterator("person"):
tempDict = each.attrib
for childNode in each.getchildren(): # 利用getchildren方法得到子節點
tempDict[childNode.tag] = childNode.text # tag獲取節點名,text獲取節點值
tempJson = json.dumps(tempDict,ensure_ascii=False)
print(tempJson)
f.write(tempJson + '\n')
f.close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM