Hướng dẫn what is xmltodict python?
Today we will learn how to convert XML to JSON and XML to Dict in python. We can use python Show Converting XML to Dict/JSONXML files have slowly become obsolete but there are pretty large systems on the web that
still uses this format. XML is heavier than JSON and so, most developers prefer the latter in their applications. When applications need to understand the XML provided by any source, it can be a tedious task to convert it to JSON. The Getting started with xmltodictWe can get started with Install xmltodict moduleHere is how we can install the xmltodict module using Python Package Index (pip):
This will be
done quickly as apt
tool:
Another plus point is that this module has an official Debian package. Python XML to JSONThe best place to start trying this module will be to perform an operation it was made to perform primarily, to perform XML to JSON conversions. Let’s look at a code snippet on how this can be done:
Let’s see the output for this program: Here, we simply use theparse(...) function to convert XML data to JSON and then we use the json module to print JSON in a better format.Converting XML File to JSONKeeping XML data in the code itself is neither always possible nor it is realistic. Usually, we keep our data in either database or some files. We can directly pick files and convert them to JSON as well. Let’s look at a code snippet how we can perform the conversion with an XML file:
Let’s see the output for this program: Here, we used another module pprint to print the output in a formatted manner. Apart from that, using theopen(...) function was straightforward, we used it get a File descriptor and then parsed the file into a JSON
object.Python XML to DictAs the module name suggest itself, xmltodict actually converts the XML data we provide to just a simply Python dictionary. So, we can simply access the data with the dictionary keys as well. Here is a sample program:
Let’s see the output for this program: So, the tags can be used as the keys along with the attribute keys as well. The attribute keys just need to be prefixed with the@ symbol.Supporting Namespaces in XMLIn XML data, we usually have a set of namespaces which defines the scope of the data provided by the XML file. While converting to the JSON format, it is then necessary that these namespaces persist in the JSON format as well. Let us consider this sample XML file:
Here is a sample program on how we can include XML namespaces in the JSON format as well:
Let’s see the output for this program: JSON to XML conversionALthough converting from XML to JSON is the prime objective of this module, xmltodict also supports doing the reverse operation, converting JSON to XML form. We will provide the JSON data in program itself. Here is a sample program:
Let’s see the output for this program: Please note that giving a single JSON key is necessary for this to work correctly. If we consider that we modify our program to contain multiple JSON keys at the very first level of data like:
In this case, we have three keys at the root level. If we try to unparse this form of JSON, we will face this error: This happens because xmltodict needs to construct the JSON with the very first key as the root XML tag. This means that there should only be a single JSON key at the root level of data.ConclusionIn this lesson, we studied an excellent Python module which can be used to parse and convert XML to JSON and vice-versa. We also learned how to convert XML to Dict using xmltodict module. Reference: API Doc |