How can I compare two XML files for equivalency in python?

Posted on 2007-10-17
Last Modified: 2008-01-09
I would like to be able to compare two xml files from python. This is being used to ensure that an answer received from a web service (SOAP call)  matches predefined expected output. Currently, if there is extra space between xml tags or if any of the the child xml tags are not in the exact same order, this check fails. Alternatively, finding another way to accomplish the same task (check response from a SOAP call against an expected result) is also welcome.

Thank you,
Question by:alex_georgescu
    1 Comment
    LVL 28

    Accepted Solution

    It is not obvious from the question whether you want to check only the content of the XML file or also the structure. If the structure should be checked, look for the XML validation tools. Just the first result of searching (a bit old, anyway)
     "Validating XML with External DTDs using xmlproc"

    For the content, you may be interested in parsing the XML file using the xml.etree.ElementTree that is capable also to build a dictionary structure mapping the id's to elements. This way the order of elements is not important and the elements can be easily accessed and processed (compared). See

    8.13 xml.etree.ElementTree -- The ElementTree XML API
    8.13.1 Functions

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now