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