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()

 

posted @ 2018-10-19 16:10  邪狂  阅读(6420)  评论(0编辑  收藏  举报
柔柔弱弱