How to store XML data into a MySQL database using Python? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we are going to store XML data into the MySQL database using python through XAMPP server. So we are taking student XML data and storing the values into the database. RequirementsXAMPP server: It is a cross-platform web server used to develop and test programs on a local server. It is developed and managed by Apache Friends and is open-source. It has an Apache HTTP Server, MariaDB, and interpreter for 11 different programming languages like Perl and PHP. XAMPP Stands for cross-platform, Apache, MySQL, PHP, and Perl. It can be easily installed from here.MySQL connector: MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Python Database API Specification v2.0 (PEP 249). It uses the Python standard library and has no dependencies. It can be installed using the below command:pip install mysql.connectorApproachStart XAMPP serverCreate XML file. XML Structure: <root> <child> <subchild>.....</subchild> </child> </root> We are going to use the XML module. xml.etree.ElementTree This is an Element Tree XML API that is used to implement a simple and efficient API for parsing and creating XML data. So we need to import this module. Syntax: import xml.etree.ElementTree The XML file to be created is names vignan.xml. XML <?xml version="1.0"?> <studentdata> <student> <name>Sravan Kumar</name> <id>7058</id> <department>IT</department> </student> <student> <name>Meghana</name> <id>7034</id> <department>IT</department> </student> <student> <name>Pranathi</name> <id>7046</id> <department>EEE</department> </student> <student> <name>Durga</name> <id>7078</id> <department>Mech</department> </student> <student> <name>Ishitha</name> <id>7093</id> <department>MBA</department> </student> </studentdata> For Python code:Create a python file names a.py.Import required module.Establish connection.Read XML file.Retrieve data from the XML and insert it into a table in the database.Display message on successful insertion of data. Python # import xml element tree import xml.etree.ElementTree as ET # import mysql connector import mysql.connector # give the connection parameters # user name is root # password is empty # server is localhost # database name is database conn = mysql.connector.connect(user='root', password='', host='localhost', database='database') # reading xml file , file name is vignan.xml tree = ET.parse('vignan.xml') # in our xml file student is the root for all # student data. data2 = tree.findall('student') # retrieving the data and insert into table # i value for xml data #j value printing number of # values that are stored for i, j in zip(data2, range(1, 6)): name = i.find('name').text id = i.find('id').text department = i.find('department').text # sql query to insert data into database data = """INSERT INTO vignan(name,id,department) VALUES(%s,%s,%s)""" # creating the cursor object c = conn.cursor() # executing cursor object c.execute(data, (name, id, department)) conn.commit() print("vignan student No-", j, " stored successfully") Output: Save both filesVerify the content of the table in which the values were recently inserted. Create Quiz Comment S sravankumar_171fa07058 Follow 0 Improve S sravankumar_171fa07058 Follow 0 Improve Article Tags : Python Python-mySQL Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like