Open In App

Compare Two Xml Files in Python

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads