• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Convert YAML to XML in Python

Convert YAML to XML in Python

Author: Aditya Raj
Last Updated: February 17, 2023

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.

Table of Contents
  1. What is the YAML File Format?
  2. What is XML File Format?
  3. Convert YAML String to XML String in Python
  4. Convert YAML String to XML File in Python
  5. Convert YAML File to an XML String
  6. Convert YAML File to XML File in Python
  7. Conclusion

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. The open() 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.  The unparse() method takes the dictionary as its first input argument and the file pointer as the input argument to the output 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.

Output XML File
Output XML File

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.

YAML File
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. The open() 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. The load() method takes the file pointer as its first input argument and the loader type as input to its Loader 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. The unparse() 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!

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us