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

Posted on 2007-10-17
Medium Priority
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 29

Accepted Solution

pepr earned 2000 total points
ID: 20099025
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 http://docs.python.org/lib/module-xml.etree.ElementTree.html
8.13.1 Functions http://docs.python.org/lib/elementtree-functions.html#l2h-1977

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…
Suggested Courses

864 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