In this example, we demonstrate how to create an XML file programmatically using Python. The code processes a list of dictionaries, where each dictionary represents a row of data, and converts it into a structured XML file. Here’s the step-by-step explanation:
import xml.etree.ElementTree as ET
Step 1: Import the ElementTree module, which is used for building and processing XML data in Python.
# Data (representing the provided table)
data = [
{"id": 1, "name": "John Deo", "class": "Four", "mark": 75, "gender": "female"},
{"id": 2, "name": "Max Ruin", "class": "Three", "mark": 85, "gender": "male"},
...
]
Step 2: Define the data to be converted into XML. Here, the data is represented as a list of dictionaries, where each dictionary contains information about a student (e.g., ID, name, class, mark, and gender).
# Create the root element
root = ET.Element("students")
Step 3: Create the root element of the XML file using ET.Element(). In this case, the root element is <students>.
# Add data to the XML
for row in data:
student = ET.SubElement(root, "student")
for key, value in row.items():
child = ET.SubElement(student, key)
child.text = str(value)
Step 4: Iterate through each row of data. For each row:
# Generate the XML tree
tree = ET.ElementTree(root)
# Save the XML to a file
output_file = "students.xml"
tree.write(output_file, encoding="utf-8", xml_declaration=True)
print(f"XML file '{output_file}' created successfully!")
Step 5: Use ET.ElementTree to build the complete XML tree and save it to a file named students.xml. The encoding="utf-8" ensures the file supports international characters, and xml_declaration=True adds the XML declaration at the top of the file.
<students>
<student>
<id>1</id>
<name>John Deo</name>
<class>Four</class>
<mark>75</mark>
<gender>female</gender>
</student>
<student>
<id>2</id>
<name>Max Ruin</name>
<class>Three</class>
<mark>85</mark>
<gender>male</gender>
</student>
...
</students>
This code demonstrates how Python simplifies the process of generating XML files for structured data. You can customize it further to suit your specific requirements.
import xml.etree.ElementTree as ET
# Data (representing the provided table)
data = [
{"id": 1, "name": "John Deo", "class": "Four", "mark": 75, "gender": "female"},
{"id": 2, "name": "Max Ruin", "class": "Three", "mark": 85, "gender": "male"},
{"id": 3, "name": "Arnold", "class": "Three", "mark": 55, "gender": "male"},
{"id": 4, "name": "Krish Star", "class": "Four", "mark": 60, "gender": "female"},
{"id": 5, "name": "John Mike", "class": "Four", "mark": 60, "gender": "female"},
{"id": 6, "name": "Alex John", "class": "Four", "mark": 55, "gender": "male"},
{"id": 7, "name": "My John Rob", "class": "Fifth", "mark": 78, "gender": "male"},
{"id": 8, "name": "Asruid", "class": "Five", "mark": 85, "gender": "male"},
{"id": 9, "name": "Tes Qry", "class": "Six", "mark": 78, "gender": "male"},
{"id": 10, "name": "Big John", "class": "Four", "mark": 55, "gender": "female"},
{"id": 11, "name": "Ronald", "class": "Six", "mark": 89, "gender": "female"},
{"id": 12, "name": "Recky", "class": "Six", "mark": 94, "gender": "female"},
{"id": 13, "name": "Kty", "class": "Seven", "mark": 88, "gender": "female"},
{"id": 14, "name": "Bigy", "class": "Seven", "mark": 88, "gender": "female"},
{"id": 15, "name": "Tade Row", "class": "Four", "mark": 88, "gender": "male"},
{"id": 16, "name": "Gimmy", "class": "Four", "mark": 88, "gender": "male"},
{"id": 17, "name": "Tumyu", "class": "Six", "mark": 54, "gender": "male"},
{"id": 18, "name": "Honny", "class": "Five", "mark": 75, "gender": "male"},
{"id": 19, "name": "Tinny", "class": "Nine", "mark": 18, "gender": "male"},
{"id": 20, "name": "Jackly", "class": "Nine", "mark": 65, "gender": "female"},
{"id": 21, "name": "Babby John", "class": "Four", "mark": 69, "gender": "female"},
{"id": 22, "name": "Reggid", "class": "Seven", "mark": 55, "gender": "female"},
{"id": 23, "name": "Herod", "class": "Eight", "mark": 79, "gender": "male"},
{"id": 24, "name": "Tiddy Now", "class": "Seven", "mark": 78, "gender": "male"},
{"id": 25, "name": "Giff Tow", "class": "Seven", "mark": 88, "gender": "male"},
{"id": 26, "name": "Crelea", "class": "Seven", "mark": 79, "gender": "male"},
{"id": 27, "name": "Big Nose", "class": "Three", "mark": 81, "gender": "female"},
{"id": 28, "name": "Rojj Base", "class": "Seven", "mark": 86, "gender": "female"},
{"id": 29, "name": "Tess Played", "class": "Seven", "mark": 55, "gender": "male"},
{"id": 30, "name": "Reppy Red", "class": "Six", "mark": 79, "gender": "female"},
{"id": 31, "name": "Marry Toeey", "class": "Four", "mark": 88, "gender": "male"},
{"id": 32, "name": "Binn Rott", "class": "Seven", "mark": 90, "gender": "female"},
{"id": 33, "name": "Kenn Rein", "class": "Six", "mark": 96, "gender": "female"},
{"id": 34, "name": "Gain Toe", "class": "Seven", "mark": 69, "gender": "male"},
{"id": 35, "name": "Rows Noump", "class": "Six", "mark": 88, "gender": "female"},
]
# Create the root element
root = ET.Element("students")
# Add data to the XML
for row in data:
student = ET.SubElement(root, "student")
for key, value in row.items():
child = ET.SubElement(student, key)
child.text = str(value)
# Generate the XML tree
tree = ET.ElementTree(root)
# Save the XML to a file
output_file = "students.xml"
output_file = "F:\\testing\\sqlite\\" + output_file # Path to save file
tree.write(output_file, encoding="utf-8", xml_declaration=True)
print(f"XML file '{output_file}' created successfully!")
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.