SoFunction
Updated on 2025-03-02

Detailed explanation of the xmltodict module in Python

xmltodict

This seems to be a third-party dependency package, and you need to install it yourself with pip before you can use it.

pip3 install xmltodict

As for what this module is for, you can actually guess it by looking at the name. Generally, we may use json and yaml to convert to dict, but it may not be used to transfer xml to dict, but we can still take a look.

xml string parsing

Let’s take a simple xml first and take a look.

<?xml version='1.0' encoding='utf-8'?>
<project>
    <artifact>
        <versionType>BVersion</versionType>
        <repoType>Generic</repoType>
        <id>
            <offering>openEuler</offering>
            <version>openEuler 1.1.T1.B010</version>
        </id>
        <isClear>Y</isClear>
        <copies>
            <copy>
                <source>/dist</source>
                <dest></dest>
            </copy> 
        </copies>
    </artifact>
</project>

parse xml

import xmltodict
import json
xml_result = open('', 'r')
xml_dict = (xml_result.read())
print(type(xml_dict))
json_str = (xml_dict, indent=2)
print(json_str)

The output result (for the convenience of looking, I converted the result dictionary string into an indented form). From the following results, we can see that the dictionary obtained by parsing is actually an ordered dictionary.

You might ask why is an ordered dictionary rather than a normal dictionary?

This is of course to keep the order of the dictionary consistent with the order of xml elements (after all, if the order of the element of the xml file changes, it will no longer be the original xml file).

<class ''>
{
  "project": {
    "artifact": {
      "versionType": "BVersion",
      "repoType": "Generic",
      "id": {
        "offering": "openEuler",
        "version": "openEuler 1.1.T1.B010"
      },
      "isClear": "Y",
      "copies": {
        "copy": {
          "source": "/dist",
          "dest": null
        }
      }
    }
  }
}

dict to xml string

import xmltodict
xml_dict = {
  "project": {
    "artifact": {
      "versionType": "BVersion",
      "repoType": "Generic",
      "id": {
        "offering": "openEuler",
        "version": "openEuler 1.1.T1.B010"
      },
      "isClear": "Y",
      "copies": {
        "copy": {
          "source": "/dist",
          "dest": None
        }
      }
    }
  }
}
xml_str = (xml_dict, pretty=True)
print(xml_str)

Output result.

<?xml version="1.0" encoding="utf-8"?>
<project>
	<artifact>
		<versionType>BVersion</versionType>
		<repoType>Generic</repoType>
		<id>
			<offering>openEuler</offering>
			<version>openEuler 1.1.T1.B010</version>
		</id>
		<isClear>Y</isClear>
		<copies>
			<copy>
				<source>/dist</source>
				<dest></dest>
			</copy>
		</copies>
	</artifact>
</project>

This is the end of this article about the detailed explanation of the xmltodict module in Python. For more related xmltodict content in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!