Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to compare two lists in Python?
Next article icon

Compare Two Xml Files in Python

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
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.

Next Article
How to compare two lists in Python?

G

gpancomputer
Improve
Article Tags :
  • Python
  • Python Programs
  • Python-XML
Practice Tags :
  • python

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 contains Name,Age,CityJohn,25,New YorkEmily,30,Los AngelesMichael,40,Chicago fi
    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. [GFGTABS] Python d1 = {'a': 1, 'b
    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. [GFGTABS] Python a = [1, 2, 3, 4, 5] b = [1, 2
    3 min read
  • Python | Compare tuples
    Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it's corresponding index in other tuple. This can have many possible applications. Let's discuss certain ways in which this task can be performed. Method #1 : U
    4 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
  • Check If a Text File Empty in Python
    Before performing any operations on your required file, you may need to check whether a file is empty or has any data inside it. An empty file is one that contains no data and has a size of zero bytes. In this article, we will look at how to check whether a text file is empty using Python. Check if
    4 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. Usin
    3 min read
  • Python | Merge two text files
    Given two text files, the task is to merge the data and store in a new text file. Let's see how can we do this task using Python. To merge two files in Python, we are asking user to enter the name of the primary and second file and make a new file to put the unified content of the two data into this
    2 min read
  • Check end of file in Python
    In Python, checking the end of a file is easy and can be done using different methods. One of the simplest ways to check the end of a file is by reading the file's content in chunks. When read() method reaches the end, it returns an empty string. [GFGTABS] Python f = open("file.txt",
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences