import xmltodict
import json
def xml2dict():
file_object = open('1111111018378_20181203214626.xml',encoding = 'utf-8')
try:
all_the_xmlStr = file_object.read()
finally:
file_object.close()
convertedDict = xmltodict.parse(all_the_xmlStr)
jsonStr = json.dumps(convertedDict,ensure_ascii=False)
print( jsonStr);
with open('./1111111018378_20181203214626.json', 'w',encoding = 'utf-8') as f:
f.write(jsonStr.replace('@', ''))
dictVal = {
'page': {
'title': 'xxx',
'ns': 0,
'revision': {
'id': 123456,
}
}
}
convertedXml = xmltodict.unparse(dictVal);
print ("convertedXml=",convertedXml)
if __name__ == '__main__':
xml2dict();