How to insert XML data to MongoDB using Python
In this article, you will learn how to insert XML data in MongoDB using the Python programming language.
XML (Extensible Markup Language) gives a standard method to access information, making it simpler for applications and devices to utilize, store, send, and show information. It is also used for many aspects, like business-to-business transactions, e-commerce applications, generating metadata, and so on. The data stored in XML is in a formatted way that can be easily rendered. That's why it is generally used in web services and data transport and is easy to search for and understand.
As the information in XML format is not readable by general customers, we may need to transform it so that we can easily display it in web applications where we want. MongoDB is a NoSQL (Not only SQL) open-source document-oriented database management system. This is suitable for modern internet applications. It provides dynamic queries, scalability, secondary indexes, and fast atomic updates, and we can easily use more than one database in a project.
Parse XML File
Python provides the ElementTree module to parse the XML. Here is a simple application that parses some given XML data and extracts the data elements from the XML.
Suppose we have the following student.xml file-
<?xml version="1.0"?>
<school>
<student>
<name>Alaya</name>
<age>12</age>
<section>6A</section>
</student>
<student>
<name>Carle</name>
<age>14</age>
<section>7C</section>
</student>
<student>
<name>Amma</name>
<age>11</age>
<section>5B</section>
</student>
</school>
Here, we have imported the XML data by reading from an XML file. The parse() method parses an XML section into an element tree and the findall() method traverses the immediate children of the referenced element. We have traversed all the immediate children of the 'student' element and stored in a variable called 'stud'.
import xml.etree.ElementTree as ET
tree = ET.parse('student.xml')
stud = tree.findall('student')
Next, we loop over the 'stud' variable. The find() method finds the first child with a particular tag, and the Element.text accesses the element's text content.
for ep in stud:
name = ep.find('name').text
age = ep.find('age').text
section = ep.find('section').text
Connect to MongoDB using Python
Next, we need the MongoDB connector module to connect the database. If you do not have this installed, you can install MongoDB Connector using the pip command.
(env) c:\python37\Scripts\projects>pip install pymongo
On successful installation, it returns something like this-
Collecting pymongo
Downloading pymongo-3.10.1-cp37-cp37m-win_amd64.whl (354 kB)
|████████████████████████████████| 354 kB 284 kB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.10.1
Once installed, use the following command in the terminal window to start the mongodb daemon.
$ mongod
In the python file, we need to import the MongoDB connector to connect to the database. After importing, create an instance of MongoClient and establish a connection to the default host, i.e., localhost and port 27017.
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
Here, we have created a new database 'school'.
mydb = client["school"]
To insert a single document into a collection, the insert() method is used-
stu_dict = [
{'name': name, 'age': age, 'section': section}
]
x = mycol.insert(stu_dict)
Complete Code: To insert XML data in MongoDB using Python
At the start of this article, we have explained the code in chunks. Here is the complete code to insert XML data in MongoDB using Python.
import pymongo
import xml.etree.ElementTree as ET
client = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = client["school"]
mycol = mydb["students"]
tree = ET.parse('student.xml')
stud = tree.findall('student')
for ep in stud:
name = ep.find('name').text
age = ep.find('age').text
section = ep.find('section').text
stu_dict = [
{'name': name, 'age': age, 'section': section}
]
x = mycol.insert(stu_dict)
for y in mycol.find():
print(y)
The above code returns the following output-
{'_id': ObjectId('5e81e80da4e3a7c5b486c68b'), 'name': 'Alaya', 'age': '12', 'section': '6A'}
{'_id': ObjectId('5e81e80da4e3a7c5b486c68c'), 'name': 'Carle', 'age': '14', 'section': '7C'}
{'_id': ObjectId('5e81e80da4e3a7c5b486c68d'), 'name': 'Amma', 'age': '11', 'section': '5B'}
Related Articles
Insert JSON data into MongoDB using PythonConvert MongoDB Document to JSON using Python
Display data from MongoDB in HTML table using Python
CRUD operations in Python using MongoDB connector
Read xml file Python
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python get visitor information by IP address
Python Webbrowser
Python Tkinter Overview and Examples
Python Turtle Graphics Overview
Factorial Program in Python
Python snake game code with Pygame
Python JSON Tutorial - Create, Read, Parse JSON file
Python convert xml to dict
Python convert dict to xml