Python - Which is faster to parse Json or XML? [closed]

笑着哭i 提交于 2019-12-23 07:38:52

问题


From my observations overall JSON is faster to Parse than XML. I have found two good question regarding this. One is asked for PHP and other is asked for JavaScript. I want to know about python, how python is efficient with them? and which is more efficient to parse. Also please help in choosing the best Python parser for XML (e.g. xmlparser library , lxml or ?) and JSON (simplejson, jsonlib or ?).


回答1:


In my opinion, it does not make sense to compare XML and JSON parsing times. Choosing one format over the other depends on your use case.

If you only want to store primitive types as supported by JSON in a simple, human-readable format, JSON is the way to go. If you need all the power and complexity of a markup language, use XML. You probably don't want to invent a document format based on JSON.

The bottleneck with parsing JSON and XML usually is not the parsing itself, but the interpretation/representation of the data. An event-based XML parser usually is very fast, but building a complex DOM tree of thousands of small objects is not. If you need to parse XML to nested native data structures such as lists and dictionaries, the slow part will be the interpretation of the parsing results, not the actual string analysis. Since JSON parses right to those primitive types rather than a complex object tree, it will likely be faster.



来源:https://stackoverflow.com/questions/7818030/python-which-is-faster-to-parse-json-or-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!