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