Contents

Manipulating XML in Python

A Practical Guide to Manipulating and Modifying XML Files in Python

Website Visitors:

Introduction

XML (eXtensible Markup Language) is a widely used format for storing and transmitting data. In Python, there are several libraries available for manipulating XML, allowing developers to parse, create, modify, and write XML files. In this article, we will explore how to manipulate XML in Python, focusing on the xml.etree.ElementTree module and the xml.dom.minidom module. We will cover the basics of XML parsing, creating new XML documents, modifying existing XML files, and more. Let’s dive in!

Parsing XML with xml.etree.ElementTree: The xml.etree.ElementTree module provides a simple and efficient way to parse and manipulate XML documents. Here’s an example of how to parse an XML file using this module:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import xml.etree.ElementTree as ET

# Parse an XML file
tree = ET.parse('data.xml')
root = tree.getroot()

# Access elements and attributes
print("Root tag:", root.tag)
for child in root:
    print("Child tag:", child.tag, "Child text:", child.text)
    print("Child attribute:", child.get('attribute_name'))

Creating and Modifying XML with xml.etree.ElementTree

To create a new XML document or modify an existing one, we can use the methods provided by xml.etree.ElementTree. Here are some common operations:

  1. Creating a new XML document:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import xml.etree.ElementTree as ET

# Create a new XML document
root = ET.Element("root")
tree = ET.ElementTree(root)

# Add child elements
child1 = ET.SubElement(root, "child1")
child2 = ET.SubElement(root, "child2")

# Add attributes to elements
child1.set("attribute_name", "attribute_value")

# Write the XML document to a file
tree.write("new_data.xml")
  1. Modifying existing XML:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import xml.etree.ElementTree as ET

# Parse an existing XML file
tree = ET.parse('data.xml')
root = tree.getroot()

# Modify elements and attributes
root.find("child1").text = "New text"
root.find("child2").set("attribute_name", "new_value")

# Write the modified XML to a file
tree.write("modified_data.xml")

Using xml.dom.minidom for XML Manipulation

The xml.dom.minidom module provides another way to manipulate XML in Python. It is part of the Python standard library and offers a simple and lightweight approach to working with XML. Here’s an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import xml.dom.minidom as minidom

# Parse an XML file
doc = minidom.parse('data.xml')

# Access elements and attributes
root = doc.documentElement
print("Root tag:", root.tagName)

# Access child elements
children = root.getElementsByTagName("child")
for child in children:
    print("Child tag:", child.tagName, "Child text:", child.firstChild.nodeValue)
    print("Child attribute:", child.getAttribute('attribute_name'))

# Create a new XML element
new_element = doc.createElement("new_element")
new_element.setAttribute("attribute_name", "attribute_value")
root.appendChild(new_element)

# Write the modified XML to a file
with open("modified_data.xml", "w") as file:
    doc.writexml(file)

Suggested Article

If you’d like to continue reading, checkout our other articles on python here or browse all other topics here.

Conclusion

Manipulating XML in Python is essential when working with XML data. In this article, we explored two popular modules, xml.etree.ElementTree and xml.dom.minidom, for parsing, creating, and modifying XML documents. The examples provided should serve as a starting point for your XML manipulation needs. Remember to choose the module that best fits your requirements and explore their documentation for more advanced operations. With these powerful tools at your disposal, you can efficiently handle XML data in Python.

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.