Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create XML Documents using Python
XML documents are essential for data exchange between systems. Python provides the xml.etree.ElementTree library for creating and manipulating XML documents easily. Let's explore how to create both simple and complex XML structures using Python.
Basic XML Document Creation
Setting Up ElementTree
First, import the ElementTree library and create a root element ?
import xml.etree.ElementTree as ET
# Create root element
root = ET.Element('root')
# Create child elements
person = ET.SubElement(root, 'person')
name = ET.SubElement(person, 'name')
age = ET.SubElement(person, 'age')
# Set text content
name.text = 'John Doe'
age.text = '30'
# Create and write XML file
tree = ET.ElementTree(root)
tree.write('person.xml')
# Display the XML structure
ET.dump(root)
<root><person><name>John Doe</name><age>30</age></person></root>
Creating XML with Attributes
Elements can have attributes for additional metadata ?
import xml.etree.ElementTree as ET
# Create order root element
root = ET.Element('order')
# Customer element with attributes
customer = ET.SubElement(root, 'customer', name='John Doe', address='123 Main St')
# Order details with attributes
order_details = ET.SubElement(root, 'order_details', date='2023-01-01', id='12345')
# Items container
items = ET.SubElement(order_details, 'items')
# Individual items with attributes
item1 = ET.SubElement(items, 'item', code='A100', description='Product A', quantity='5')
item2 = ET.SubElement(items, 'item', code='B200', description='Product B', quantity='10')
# Display the XML structure
ET.dump(root)
<order><customer address="123 Main St" name="John Doe" /><order_details date="2023-01-01" id="12345"><items><item code="A100" description="Product A" quantity="5" /><item code="B200" description="Product B" quantity="10" /></items></order_details></order>
Pretty Printing XML
For better readability, you can format the XML with proper indentation ?
import xml.etree.ElementTree as ET
from xml.dom import minidom
# Create a simple XML structure
root = ET.Element('library')
book1 = ET.SubElement(root, 'book', id='1')
title1 = ET.SubElement(book1, 'title')
title1.text = 'Python Programming'
author1 = ET.SubElement(book1, 'author')
author1.text = 'John Smith'
book2 = ET.SubElement(root, 'book', id='2')
title2 = ET.SubElement(book2, 'title')
title2.text = 'Data Science'
author2 = ET.SubElement(book2, 'author')
author2.text = 'Jane Doe'
# Convert to string and pretty print
xml_str = ET.tostring(root, encoding='unicode')
dom = minidom.parseString(xml_str)
print(dom.toprettyxml(indent=' '))
<?xml version="1.0" ?>
<library>
<book id="1">
<title>Python Programming</title>
<author>John Smith</author>
</book>
<book id="2">
<title>Data Science</title>
<author>Jane Doe</author>
</book>
</library>
Key Methods and Properties
| Method/Property | Description | Example |
|---|---|---|
Element() |
Creates root element | ET.Element('root') |
SubElement() |
Creates child element | ET.SubElement(parent, 'child') |
.text |
Sets element text content | element.text = 'value' |
ElementTree() |
Creates tree object | ET.ElementTree(root) |
.write() |
Saves to file | tree.write('file.xml') |
Complete Example: Employee Database
import xml.etree.ElementTree as ET
from xml.dom import minidom
# Create employee database XML
root = ET.Element('employees')
# Employee 1
emp1 = ET.SubElement(root, 'employee', id='001')
name1 = ET.SubElement(emp1, 'name')
name1.text = 'Alice Johnson'
dept1 = ET.SubElement(emp1, 'department')
dept1.text = 'Engineering'
salary1 = ET.SubElement(emp1, 'salary', currency='USD')
salary1.text = '75000'
# Employee 2
emp2 = ET.SubElement(root, 'employee', id='002')
name2 = ET.SubElement(emp2, 'name')
name2.text = 'Bob Wilson'
dept2 = ET.SubElement(emp2, 'department')
dept2.text = 'Marketing'
salary2 = ET.SubElement(emp2, 'salary', currency='USD')
salary2.text = '65000'
# Pretty print
xml_str = ET.tostring(root, encoding='unicode')
dom = minidom.parseString(xml_str)
print(dom.toprettyxml(indent=' '))
<?xml version="1.0" ?>
<employees>
<employee id="001">
<name>Alice Johnson</name>
<department>Engineering</department>
<salary currency="USD">75000</salary>
</employee>
<employee id="002">
<name>Bob Wilson</name>
<department>Marketing</department>
<salary currency="USD">65000</salary>
</employee>
</employees>
Best Practices
Use descriptive element and attribute names
Keep XML structure simple and logical
Use attributes for metadata, text content for data
Validate XML structure before saving
Use proper encoding when writing files
Conclusion
Python's ElementTree library provides a straightforward way to create XML documents. Use Element() for root elements, SubElement() for children, and the .text property for content. This approach works well for both simple and complex XML structures.
