How to Convert lists to XML in Python?


Extensible Markup Language (XML) is a popular data exchange format used in many applications. It provides a standardised way of representing data that can be easily understood by both humans and machines. In many cases, it is necessary to convert data stored in Python lists to XML format for various purposes, such as data exchange or storage.

In this article, we will explore different approaches for converting Python lists to XML format using Python's built−in libraries.

Below are the two different approaches that we can use to convert Python lists to XML in points.

Using ElementTree library

  • Import the xml.etree.ElementTree library.

  • Create an instance of ElementTree.Element for the root element of the XML tree.

  • Iterate over the list and create child elements for each item in the list.

  • Add the child elements to the root element.

  • Use the ElementTree.ElementTree object to write the XML data to a file or string.

Consider the code shown below for the above approach.

Example

import xml.etree.ElementTree as ET

# Create a list
my_list = ['apple', 'banana', 'orange']

# Create the root element of the XML tree
root = ET.Element('fruits')

# Iterate over the list and create child elements
for item in my_list:
	fruit = ET.SubElement(root, 'fruit')
	fruit.text = item

# Use the ElementTree object to write the XML data to a file or string
tree = ET.ElementTree(root)
tree.write('fruits.xml')

Explanation

  • The code first imports the xml.etree.ElementTree module, which is included in the Python standard library.

  • A Python list my_list is created with three string elements.

  • The code creates a root element for the XML tree using the Element() method of ElementTree and assigns it to the variable root. The argument passed to Element() is the tag name of the root element.

  • The code then iterates over each item in the list using a for loop. For each item, it creates a child element under the root element using the SubElement() method of the root object. The first argument passed to SubElement() is the tag name of the child element. The second argument, item, is the text content of the child element.

  • Finally, the code uses the ElementTree() method of ElementTree to create an ElementTree object with root as the root element. The write() method of ElementTree is then called to write the XML data to a file named fruits.xml.

Output

<fruits>
  <fruit>apple</fruit>
  <fruit>banana</fruit>
  <fruit>orange</fruit>
</fruits>

Using lxml library

  • Import the lxml library.

  • Create an instance of lxml.Element for the root element of the XML tree.

  • Iterate over the list and create child elements for each item in the list.

  • Add the child elements to the root element.

  • Use the lxml.etree.tostring() method to convert the XML tree to a string.

Consider the code shown below.

Example

import xml.etree.ElementTree as ET

# Create a list
my_list = ['apple', 'banana', 'orange']

# Create the root element of the XML tree
root = ET.Element('fruits')

# Iterate over the list and create child elements
for item in my_list:
	fruit = ET.SubElement(root, 'fruit')
	fruit.text = item

# Use the ElementTree object to write the XML data to a file or string
tree = ET.ElementTree(root)
tree.write('fruits.xml')

Explanation

  • The code first imports the xml.etree.ElementTree module, which is included in the Python standard library.

  • A Python list my_list is created with three string elements.

  • The code creates a root element for the XML tree using the Element() method of ElementTree and assigns it to the variable root. The argument passed to Element() is the tag name of the root element.

  • The code then iterates over each item in the list using a for loop. For each item, it creates a child element under the root element using the SubElement() method of the root object. The first argument passed to SubElement() is the tag name of the child element. The second argument, item, is the text content of the child element.

  • Finally, the code uses the ElementTree() method of ElementTree to create an ElementTree object with root as the root element. The write() method of ElementTree is then called to write the XML data to a file named fruits.xml.

Output

<fruits>
  <fruit>apple</fruit>
  <fruit>banana</fruit>
  <fruit>orange</fruit>
</fruits>

Conclusion

Both approaches that we discussed in this article involve creating an XML tree structure from the list, with each element in the list corresponding to a child element in the XML tree.

Updated on: 03-Aug-2023

570 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements