XML files are used to store and transmit data between the software systems. While developing software in python, you might also need to convert a python object to XML format for storing or transmitting the data. This article discusses how to convert a python dictionary into an XML string or File.
What is XML File Format?
XML (Extensible Markup Language) is a markup language used for encoding and structuring data in a format that is both human-readable and machine-readable. It provides a flexible way to describe and store data in a form that can be easily exchanged between different computer systems and applications.
- XML is a self-describing language, meaning that the structure of the data is defined by tags within the document, allowing for the creation of custom markup languages for specific purposes.
- It is commonly used for exchanging data over the internet, for example in the form of web services, and for storing data in a format that can be easily processed by applications.
- In an XML document, the data is stored between tags in a hierarchical structure, similar to an HTML document. Each tag can contain other tags, allowing for the creation of complex data structures. The tag names and their structure can be defined by the user, allowing for customization and flexibility in representing the data.
The syntax for storing data in XML format is as follows.
<field_name> value </field_name>
A field can have one or more fields inside itself. For instance, consider the following example.
<?xml version="1.0"?>
<employee>
<name>John Doe</name>
<age>35</age>
<job>
<title>Software Engineer</title>
<department>IT</department>
<years_of_experience>10</years_of_experience>
</job>
<address>
<street>123 Main St.</street>
<city>San Francisco</city>
<state>CA</state>
<zip>94102</zip>
</address>
</employee>
This is an example of an XML document that contains information about an employee.
- The document starts with the declaration “
<?xml version="1.0"?
>” which specifies the version of XML used in the document. - The root element of the document is the
"employee"
element, which contains all the other elements in the document. Within the"employee"
element, there are four sub-elements:"name"
,"age"
,"job"
, and"address"
. - The
"name"
element contains the name of the employee, which is"John Doe"
. The"age"
element contains the employee’s age, which is 35. - The
"job"
element contains information about the employee’s job, including the job title, department, and years of experience. - The
"address"
element contains information about the employee’s address. It has four sub-elements:"street"
,"city"
,"state"
, and"zip"
.
The corresponding python dictionary for the above file is as follows.
{"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
Now, we will discuss different ways to convert a Python Dictionary to XML format in Python.
For this, we will use two modules namely dicttoxml
, and dict2xml
modules. Both modules work in almost the same manner.
You can install the modules using PIP as shown below.
pip3 install dicttoxml dict2xml
In earlier versions of Python, you can use the following command to install the modules.
pip install dicttoxml dict2xml
We will also use the xmltodict module to convert a dictionary to XML format.
Python Dictionary to XML Using the dicttoxml Module
The dicttoxml
module provides us with the dicttoxml()
function that we can use to convert a dictionary to an XML string. The dicttoxml()
function takes a python dictionary as its input argument and returns an XML string as shown below.
import dicttoxml
python_dict={"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
print("The python dictionary is:")
print(python_dict)
xml_string=dicttoxml.dicttoxml(python_dict)
print("The XML string is:")
print(xml_string)
Output:
The python dictionary is:
{'employee': {'name': 'John Doe', 'age': '35', 'job': {'title': 'Software Engineer', 'department': 'IT', 'years_of_experience': '10'}, 'address': {'street': '123 Main St.', 'city': 'San Francisco', 'state': 'CA', 'zip': '94102'}}}
The XML string is:
b'<?xml version="1.0" encoding="UTF-8" ?><root><employee type="dict"><name type="str">John Doe</name><age type="str">35</age><job type="dict"><title type="str">Software Engineer</title><department type="str">IT</department><years_of_experience type="str">10</years_of_experience></job><address type="dict"><street type="str">123 Main St.</street><city type="str">San Francisco</city><state type="str">CA</state><zip type="str">94102</zip></address></employee></root>'
In this example, we first created a python dictionary. Then, we used the dicttoxml()
function defined in the dicttoxml
module to convert the dictionary to an XML string.
In the above output, you can observe that each item in the XML format also contains the data type of the value. If you don’t want the data type of values in the XML string, you can set the attr_type
parameter to False
in the dicttoxml()
function as shown below.
import dicttoxml
python_dict={"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
print("The python dictionary is:")
print(python_dict)
xml_string=dicttoxml.dicttoxml(python_dict,attr_type=False)
print("The XML string is:")
print(xml_string)
Output:
The python dictionary is:
{'employee': {'name': 'John Doe', 'age': '35', 'job': {'title': 'Software Engineer', 'department': 'IT', 'years_of_experience': '10'}, 'address': {'street': '123 Main St.', 'city': 'San Francisco', 'state': 'CA', 'zip': '94102'}}}
The XML string is:
b'<?xml version="1.0" encoding="UTF-8" ?><root><employee><name>John Doe</name><age>35</age><job><title>Software Engineer</title><department>IT</department><years_of_experience>10</years_of_experience></job><address><street>123 Main St.</street><city>San Francisco</city><state>CA</state><zip>94102</zip></address></employee></root>'
In this example, we have set the attr_type
parameter to False
in the dicttoxml()
function. Hence, the output XML string doesn’t contain the data type specifications.
Convert Python Dictionary to XML Using the xmltodict Module
Instead of using the dicttoxml module, you can also use the xmltodict module to convert a python dictionary to an XML string. I have already discussed how to convert an XML string to a python dictionary using the xmltodict module.
To convert a python dictionary to XML format, the xmltodict module provides us with the unparse()
function. The unparse()
function takes a dictionary as its input and converts it into an XML string as shown below.
import xmltodict
python_dict={"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
print("The python dictionary is:")
print(python_dict)
xml_string=xmltodict.unparse(python_dict)
print("The XML string is:")
print(xml_string)
Output:
The python dictionary is:
{'employee': {'name': 'John Doe', 'age': '35', 'job': {'title': 'Software Engineer', 'department': 'IT', 'years_of_experience': '10'}, 'address': {'street': '123 Main St.', 'city': 'San Francisco', 'state': 'CA', 'zip': '94102'}}}
The XML string is:
<?xml version="1.0" encoding="utf-8"?>
<employee><name>John Doe</name><age>35</age><job><title>Software Engineer</title><department>IT</department><years_of_experience>10</years_of_experience></job><address><street>123 Main St.</street><city>San Francisco</city><state>CA</state><zip>94102</zip></address></employee>
In this example, we have used the unparse()
method defined in the xmltodict module to convert a python dictionary to an XML string.
Dictionary to XML With Pretty Printing in Python
In all the above examples, the dictionary is converted into an XML string without any formatting. If you want to structure the XML string in a nice format, you can use the dict2xml module to convert the python dictionary into a pretty printed XML string. The dict2xml()
function defined in the dict2xml module takes a dictionary as its input argument and returns a pretty printed XML string as shown below.
import dict2xml
python_dict={"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
print("The python dictionary is:")
print(python_dict)
xml_string=dict2xml.dict2xml(python_dict)
print("The XML string is:")
print(xml_string)
Output:
The python dictionary is:
{'employee': {'name': 'John Doe', 'age': '35', 'job': {'title': 'Software Engineer', 'department': 'IT', 'years_of_experience': '10'}, 'address': {'street': '123 Main St.', 'city': 'San Francisco', 'state': 'CA', 'zip': '94102'}}}
The XML string is:
<employee>
<address>
<city>San Francisco</city>
<state>CA</state>
<street>123 Main St.</street>
<zip>94102</zip>
</address>
<age>35</age>
<job>
<department>IT</department>
<title>Software Engineer</title>
<years_of_experience>10</years_of_experience>
</job>
<name>John Doe</name>
</employee>
In this example, we have converted a python dictionary to an XML string using the dict2xml()
function defined in the dict2xml module. In the output, you can observe that the XML string is pretty printed. However, it doesn’t contain the header containing the XML version specifier. This means that the dict2xml()
function only converts the data into XML format. It doesn’t create a valid XML string from the data.
Convert Dictionary to XML File in Python
To convert a dictionary into an XML file, we can use two approaches. In the first approach, we can first convert the dictionary into an XML string and then save it using file operations. Alternatively, we can also directly convert the dictionary into an XML file. Let us discuss both approaches.
Dictionary to File Using XML Strings in Python
To convert a python dictionary into an XML file by first converting it into a string, we will use the following steps.
- First, we will convert the dictionary into an XML string using the
dicttoxml()
function defined in the dicttoxml module. - Next, we will open a file with a .xml extension in write mode using the
open()
function. Theopen()
function takes the file name as its first input argument and the python literal “w” as its second argument. After execution, it returns a file pointer. - Once we get the file pointer, we will write the XML string into the file using the
write()
method. Thewrite()
method, when invoked on a file pointer, takes the string as its input argument and saves the string into the file. - Finally, we will close the file using the
close()
method.
After execution of the above steps, the python dictionary will be converted into an XML file. You can observe this in the following example.
import dicttoxml
python_dict={"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
xml_string=dicttoxml.dicttoxml(python_dict,attr_type=False)
xml_file=open("person.xml","w")
xml_file.write(str(xml_string))
xml_file.close()
The output file is:
The dicttoxml()
function returns a string in byte format. Hence, the XML file is saved into the file as a byte string as you can observe in the above image.
The dicttoxml module doesn’t save the file content in good formatting. To format the XML string in the file, you can use the dict2xml module instead of the dicttoxml module as shown in the following example.
import dict2xml
python_dict={"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
xml_string=dict2xml.dict2xml(python_dict)
xml_file=open("person.xml","w")
xml_file.write(str(xml_string))
xml_file.close()
Output:
In the above image, you can observe that the dict2xml()
method returns a formatted XML string. However, it doesn’t specify XML version or encoding format. Hence, this approach also doesn’t work very well.
So, instead of converting the dictionary to XML string and then saving it to a file, we can directly save the XML formatted data to the file using the xmltodict module.
Convert Dictionary Directly to XML File in Python
We can use the unparse()
method defined in the xmltodict module to convert a python dictionary directly into an XML file. For this, we will use the following steps.
- First, we will open a file with a .xml extension in write mode using the
open()
function. Theopen()
function takes the file name as its first input argument and the literal “w” as its second argument. After execution, it returns a file pointer. - Once we get the file pointer, we will write the python into the file using the
unparse()
function defined in the xmltodict module. Theunparse()
function takes the python dictionary as its first input argument and the file pointer as the input to its output parameter. After execution, it saves the dictionary into the file as XML. - Finally, we will close the file using the
close()
method.
After execution of the above steps, the python dictionary will be converted into an XML file as shown below.
import xmltodict
python_dict={"employee":{'name': 'John Doe',
'age': '35',
'job': {'title': 'Software Engineer',
'department': 'IT',
'years_of_experience': '10'},
'address': {'street': '123 Main St.',
'city': 'San Francisco',
'state': 'CA',
'zip': '94102'}}}
xml_file=open("person.xml","w")
xmltodict.unparse(python_dict,output=xml_file)
xml_file.close()
Output:
In the above output, you can observe that we get a valid XML document when we convert a dictionary to an XML file using the xmltodict.unparse() method.
Conclusion
In this article, we have discussed different ways to convert a python dictionary into an XML string or File.
To learn more about python programming, you can read this article on how to convert a dictionary to YAML in Python. You might also like this article on custom json encoders in python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.