【背景】
看到:
所以先去试试,用python实现,将xml转为json。
【解决过程】
1.参考:
所以先以:
<student> |
为例。
2.参考:
Converting XML to JSON using Python?
去试试:
然后去安装:
D:\tmp\dev_tools\python\xml\xml2json>pip install xmltodict D:\tmp\dev_tools\python\xml\xml2json> |
结果啥输出都没有。
所以还是手动下载对应的文件吧:
xmltodict-master.zip
解压,进去安装:
D:\tmp\dev_tools\python\xml\xml2json\xmltodict-master\xmltodict-master>setup.py install D:\tmp\dev_tools\python\xml\xml2json\xmltodict-master\xmltodict-master> |
3.然后写测试代码去测试。
结果用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【折腾】Python中xml和Json之间相互转换:xml转json,json转xml Version: 2013-05-15 Author: Crifan Contact: admin (at) crifan.com """ import xmltodict; import json; def pythonXmlToJson(): """ demo Python xml to json """ xmlStr = """" <student> <stid>10213</stid> <info> <name>name</name> <mail>xxx@xxx.com</mail> <sex>male</sex> </info> <course> <name>math</name> <score>90</score> </course> <course> <name>english</name> <score>88</score> </course> </student> """ convertedDict = xmltodict.parse(xmlStr); jsonStr = json.dumps(convertedDict); print "jsonStr=" ,jsonStr; ############################################################################### if __name__ = = "__main__" : pythonXmlToJson(); |
却出错:
D:\tmp\tmp_dev_root\python\tutorial_summary\json_to_xml>json_to_xml.py File "D:\tmp\tmp_dev_root\python\tutorial_summary\json_to_xml\json_to_xml.py", line 45, in <module> pythonXmlToJson(); File "D:\tmp\tmp_dev_root\python\tutorial_summary\json_to_xml\json_to_xml.py", line 39, in pythonXmlToJson convertedDict = xmltodict.parse(xmlStr); File "D:\tmp\dev_install_root\Python27_x64\lib\site-packages\xmltodict.py", line 195, in parse parser.Parse(xml_input, True) xml.parsers.expat.ExpatError: unclosed token: line 1, column 0 |
不知道是什么原因。
4. 然后才发现,原来是把
xmlStr = """ |
误写成
xmlStr = """" |
了,所以,要处理的xml字符串的第一个字符就是“了,所以会报错。
改为真正的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【折腾】Python中xml和Json之间相互转换:xml转json,json转xml Version: 2013-05-15 Author: Crifan Contact: admin (at) crifan.com """ import xmltodict; import json; def pythonXmlToJson(): """ demo Python xml to json """ xmlStr = """ <student> <stid>10213</stid> <info> <name>name</name> <mail>xxx@xxx.com</mail> <sex>male</sex> </info> <course> <name>math</name> <score>90</score> </course> <course> <name>english</name> <score>88</score> </course> </student> """ convertedDict = xmltodict.parse(xmlStr); jsonStr = json.dumps(convertedDict); print "jsonStr=" ,jsonStr; ############################################################################### if __name__ = = "__main__" : pythonXmlToJson(); |
然后就可以正常输出了:
D:\tmp\tmp_dev_root\python\tutorial_summary\json_to_xml>json_to_xml.py }]}} |
5.但是很明显,没有格式化输出,很难看。
所以再去改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【折腾】Python中xml和Json之间相互转换:xml转json,json转xml Version: 2013-05-15 Author: Crifan Contact: admin (at) crifan.com """ import xmltodict; import json; def pythonXmlToJson(): """ demo Python xml to json """ xmlStr = """ <student> <stid>10213</stid> <info> <name>name</name> <mail>xxx@xxx.com</mail> <sex>male</sex> </info> <course> <name>math</name> <score>90</score> </course> <course> <name>english</name> <score>88</score> </course> </student> """ convertedDict = xmltodict.parse(xmlStr); jsonStr = json.dumps(convertedDict, indent = 1 ); print "jsonStr=" ,jsonStr; ############################################################################### if __name__ = = "__main__" : pythonXmlToJson(); |
输出为:
D:\tmp\tmp_dev_root\python\tutorial_summary\json_to_xml>json_to_xml.py "student": { "stid": "10213", "info": { "name": "name", "mail": "xxx@xxx.com", "sex": "male" }, "course": [ { "name": "math", "score": "90" }, { "name": "english", "score": "88" } ] } } |
6.再去试试,将json转为xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【折腾】Python中xml和Json之间相互转换:xml转json,json转xml Version: 2013-05-15 Author: Crifan Contact: admin (at) crifan.com """ import xmltodict; import json; def pythonConversionXmlAndJson(): """ demo Python conversion between xml and json """ #1.Xml to Json xmlStr = """ <student> <stid>10213</stid> <info> <name>name</name> <mail>xxx@xxx.com</mail> <sex>male</sex> </info> <course> <name>math</name> <score>90</score> </course> <course> <name>english</name> <score>88</score> </course> </student> """ convertedDict = xmltodict.parse(xmlStr); jsonStr = json.dumps(convertedDict, indent = 1 ); print "jsonStr=" ,jsonStr; #2.Json to Xml dictVal = { 'page' : { 'title' : 'King Crimson' , 'ns' : 0 , 'revision' : { 'id' : 547909091 , } } }; convertedXml = xmltodict.unparse(dictVal); print "convertedXml=" ,convertedXml; ############################################################################### if __name__ = = "__main__" : pythonConversionXmlAndJson(); |
输出为:
D:\tmp\tmp_dev_root\python\tutorial_summary\json_to_xml>json_to_xml.py "student": { "stid": "10213", "info": { "name": "name", "mail": "xxx@xxx.com", "sex": "male" }, "course": [ { "name": "math", "score": "90" }, { "name": "english", "score": "88" } ] } } convertedXml= <?xml version="1.0" encoding="utf-8"?> <page><ns>0</ns><revision><id>547909091</id></revision><title>King Crimson</title></page> |
【总结】
python中,xml和json互转的话,可以借用martinblech写的xmltodict。