Compare Two Xml Files in Python
Last Updated :
28 Apr, 2025
We are given two XML files and our task is to compare these two XML files and find out if both files are some or not by using different approaches in Python. In this article, we will see how we can compare two XML files in Python.
Compare Two XML Files in Python
Below are the possible approaches to compare two XML files in Python:
- Using lxml module
- Using xml.dom.minidom
- Using BeautifulSoup
file1.xml
XML
<geeks>
<article>
<title>Introduction to Python</title>
<author>GeeksforGeeks</author>
<content>Python is a versatile programming language.</content>
</article>
<article>
<title>Data Structures in Python</title>
<author>GeeksforGeeks</author>
<content>Learn about various data structures in Python.</content>
</article>
</geeks>
file2.xml
XML
<geeks>
<article>
<title>Introduction to Python</title>
<author>GeeksforGeeks</author>
<content>Python is a powerful and easy-to-learn programming language.</content>
</article>
<article>
<title>Algorithms in Python</title>
<author>GeeksforGeeks</author>
<content>Explore different algorithms implemented in Python.</content>
</article>
</geeks>
Compare Two XML Files Using lxml module
In this approach, we are using the lxml module to parse XML files (file1.xml and file2.xml) into ElementTree objects (tree1 and tree2). To use this module, we need to install it using pip install lxml command. We then convert these trees to their string representations using etree.tostring() and compare them. If the string representations are identical, it indicates that the XML files are the same; otherwise, they are considered different.
Python3
from lxml import etree
tree1 = etree.parse('file1.xml')
tree2 = etree.parse('file2.xml')
diff = etree.tostring(tree1) == etree.tostring(tree2)
if diff:
print("XML files are same.")
else:
print("XML files are different.")
Output:
XML files are different.
Compare Two XML Files Using xml.dom.minidom
In this approach, we are using the xml.dom.minidom module to parse XML files (file1.xml and file2.xml) into Document objects (doc1 and doc2). By converting these documents to their XML string representations using toxml(), we can directly compare the strings. If the string representations match, it indicates that the XML files are the same; otherwise, they are considered different.
Python3
from xml.dom import minidom
doc1 = minidom.parse('file1.xml')
doc2 = minidom.parse('file2.xml')
diff = doc1.toxml() == doc2.toxml()
if diff:
print("XML files are same.")
else:
print("XML files are different.")
Output:
XML files are different.
Compare Two XML Files Using BeautifulSoup
In this approach, we are using the BeautifulSoup module from the bs4 library to parse XML files (file1.xml and file2.xml). By creating BeautifulSoup objects (soup1 and soup2), we obtain their prettified string representations using prettify(). Comparing these prettified strings allows us to determine if the XML files are the same or different.
Python3
from bs4 import BeautifulSoup
with open('file1.xml', 'r') as file1, open('file2.xml', 'r') as file2:
soup1 = BeautifulSoup(file1, 'xml')
soup2 = BeautifulSoup(file2, 'xml')
if soup1.prettify() == soup2.prettify():
print("XML files are same.")
else:
print("XML files are different.")
Output:
XML files are different.
Similar Reads
Compare Two Csv Files Using Python We are given two files and our tasks is to compare two CSV files based on their differences in Python. In this article, we will see some generally used methods for comparing two CSV files and print differences.file1.csv containsName,Age,CityJohn,25,New YorkEmily,30,Los AngelesMichael,40,Chicagofile2
2 min read
How to Compare Two Dictionaries in Python In this article, we will discuss how to compare two dictionaries in Python. The simplest way to compare two dictionaries for equality is by using the == operator.Using == operatorThis operator checks if both dictionaries have the same keys and values.Pythond1 = {'a': 1, 'b': 2} d2 = {'a': 1, 'b': 2}
2 min read
How to compare two lists in Python? In Python, there might be a situation where you might need to compare two lists which means checking if the lists are of the same length and if the elements of the lists are equal or not. Let us explore this with a simple example of comparing two lists.Pythona = [1, 2, 3, 4, 5] b = [1, 2, 3, 4, 5] #
3 min read
How to Compare Two Iterators in Python Python iterators are powerful tools for traversing through sequences of elements efficiently. Sometimes, you may need to compare two iterators to determine their equality or to find their differences. In this article, we will explore different approaches to compare two iterators in Python. Compare T
3 min read
Compare Keys In Two Yaml Files And Print Differences? YAML (YAML Ain't Markup Language) files are widely used for configuring applications and storing data in a human-readable format. When dealing with multiple YAML files, it's common to compare them to identify differences, especially when managing configurations across different environments or versi
2 min read
Comparing Python Lists Without Order Sometimes we need to compare two lists without worrying about the order of elements. This is particularly useful when checking if two lists contain the same elements regardless of their arrangement. In this article, we'll explore different ways to compare Python lists without considering order.Using
3 min read