YAML and XML files are used extensively to store and transmit data in the software domain. This article discusses how to convert a YAML file or string to XML in Python.
What is the YAML File Format?
YAML is an acronym for “YAML Ain’t Markup Language”. It is a human-readable data serialization format. YAML file format is often used for configuration files, data exchange between languages, and other applications where human readability is important.
A YAML file consists of data represented as key-value pairs, lists, and nested structures. Here’s an example of a simple YAML file.
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
This YAML file represents an employee data record with the following attributes.
name
: The name of the employee is"John Doe"
.age
: The employee is 35 years old.job
: The"job"
attribute contains the employee’s job details. For this, it uses the following nested attributes.title
: The employee’s job title is “Software Engineer”.department
: The employee’s department is “IT”.years_of_experience
: The employee has 10 years of experience in their job.
address
: The"address"
attribute contains four different attributes to represent the address of the person.street
: The employee’s street address is “123 Main St.”.city
: The employee’s city is “San Francisco”.state
: The employee’s state is “CA”.zip
: The employee’s zip code is “94102”.
One of the benefits of using the YAML file format is its simplicity and readability. It uses whitespace and indentation to define its structure, which makes it easier for humans to read and edit. Additionally, YAML is supported by a wide range of programming languages. This makes it an ideal format for data exchange between different systems.
What is XML File Format?
XML stands for “Extensible Markup Language” and it is a markup language used for structuring and storing data in a hierarchical format. XML files are plain text files with tags used to define data elements and their attributes, similar to HTML.
XML is often used in web development and data exchange between different applications or systems. An XML document consists of a set of elements, which can have attributes and child elements. The root element is the top-level element that contains all other elements in the document.
If we convert the YAML data shown in the previous example, it looks as follows.
<?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>
Convert YAML String to XML String in Python
To convert a YAML string to XML, we will use the xmltodict module and the yaml module.
The yaml module provides us with the load()
method that we can use to convert the yaml string to a python dictionary. The load()
method takes the yaml string as its first input argument and the type of yaml loader as the input to the Loader
argument. After execution, it returns a python dictionary.
Once we get the python dictionary, we can convert it to an XML string using the unparse()
method defined in the xmltodict module. The unparse()
method takes the python dictionary as its input argument. After execution, it returns the corresponding XML string.
You can observe this in the following example.
import yaml
from yaml import SafeLoader
import xmltodict
yaml_string="""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 YAML string is:")
print(yaml_string)
python_dict=yaml.load(yaml_string,Loader=SafeLoader)
xml_string=xmltodict.unparse(python_dict)
print("The XML string is:")
print(xml_string)
Output:
The YAML string 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>
Convert YAML String to XML File in Python
To convert a YAMl string to an XML file, we will use the following steps.
- First, we will convert the yaml string to a python dictionary using the
load()
method defined in the yaml module. - Next, we will open an XML file in write mode using the
open()
function. Theopen()
function takes the filename as its first input argument and the python literal“w”
as its second input argument. After execution, it returns the file pointer. - Now, we will use the
unparse()
method defined in the xmltodict module to save the python dictionary as XML in the file. Theunparse()
method takes the dictionary as its first input argument and the file pointer as the input argument to theoutput
parameter. After execution, it writes the XML content to the file. - Finally, we will close the XML file using the
close()
method.
After executing the above steps, we can convert the yaml string to an XML file as shown below.
import yaml
from yaml import SafeLoader
import xmltodict
yaml_string="""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 YAML string is:")
print(yaml_string)
python_dict=yaml.load(yaml_string,Loader=SafeLoader)
file=open("person.xml","w")
xml_string=xmltodict.unparse(python_dict,output=file)
file.close()
print("XML File saved.")
Output:
The YAML string 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
XML File saved.
The output file looks as follows.
Convert YAML File to an XML String
Instead of a YAML string, we can also convert a yaml file to an XML string or file. For this, we will use the following YAML file.
To convert a YAML file to an XML string, we will use the following steps.
- First, we will open the yaml file in read mode using the
open()
method. Theopen()
function takes the file name as its first input argument and the literal“r”
as its second input argument. After execution, it returns a file pointer. - Next, we will create a python dictionary from the yaml file. For this, we will use the
load()
method defined in the yaml module. Theload()
method takes the file pointer as its first input argument and the loader type as input to itsLoader
parameter. After execution, it returns the dictionary created from the yaml file. - Once we get the dictionary, we will convert it to an XML string using the
unparse()
method. Theunparse()
method takes the dictionary as its input argument. After execution, it returns an XML string.
By executing the above steps, we can obtain an XML string from a yaml file. You can observe this in the following example.
import yaml
from yaml import SafeLoader
import xmltodict
yaml_file=open("person.yaml","r")
python_dict=yaml.load(yaml_file,Loader=SafeLoader)
xml_string=xmltodict.unparse(python_dict)
print("The XML string is:")
print(xml_string)
yaml_file.close()
Output:
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>
Convert YAML File to XML File in Python
To convert a YAML file to an XML file, we will first obtain a python dictionary from the yaml file using the load()
method as discussed in the previous section.
After obtaining the python dictionary, we will open an XML file in write mode using the open()
function. The open()
function takes the filename as its first input argument and “w”
as its second input argument. After execution, it returns the file pointer.
Once we get the file pointer, we will use the unparse()
method defined in the xmltodict module to save the python dictionary as XML in the file. Finally, we will close the file using the close()
method.
You can observe the entire process in the following example.
import yaml
from yaml import SafeLoader
import xmltodict
yaml_file=open("person.yaml","r")
python_dict=yaml.load(yaml_file,Loader=SafeLoader)
file=open("person.xml","w")
xml_string=xmltodict.unparse(python_dict,output=file)
file.close()
yaml_file.close()
Conclusion
In this article, we have discussed different ways to convert a YAML file or String to XML in Python.
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.