By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,836 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,836 IT Pros & Developers. It's quick & easy.

Comparing two minidom objects

P: n/a
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails:
import xml.dom.minidom
d1 = xml.dom.minidom.parse("ES.xml")
d2 = xml.dom.minidom.parse("ES.xml")
d1 == d2

False

My goal is to decide whether or not I need to prompt the user to save config
information at the end of a program run by generating a minidom object then
comparing it with the last saved version.

Thx,

Skip
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Skip Montanaro wrote:
I'd like to compare two xml.dom.minidom objects, but the naive attempt
fails:
import xml.dom.minidom
d1 = xml.dom.minidom.parse("ES.xml")
d2 = xml.dom.minidom.parse("ES.xml")
d1 == d2

False


You want some recursive comparision that defines equality in terms of node
and children. Something like this:

def tree_eq(a, b):
if a.nodeName == b.nodeName and len(a.childNodes) == len(b.childNodes):
res = True
for ac, bc in zip(a.childNodes, b.childNodes):
res = res and tree_eq(ac, bc)
if not res:
return False
return res
return False
The nodeName is just one property you can check for equality - maybe you
want to additionally compare nodeType and nodeValue.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
> res = res and tree_eq(ac, bc)

The aggregation is actually not necessary, as we shourtcircuit the and'ing
of the node comparisions.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #3

P: n/a
sk**@pobox.com (Skip Montanaro) wrote in message news:<72**************************@posting.google. com>...
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails:
import xml.dom.minidom
d1 = xml.dom.minidom.parse("ES.xml")
d2 = xml.dom.minidom.parse("ES.xml")
d1 == d2

False

My goal is to decide whether or not I need to prompt the user to save config
information at the end of a program run by generating a minidom object then
comparing it with the last saved version.


http://uche.ogbuji.net/tech/akara/no...tte-whitespace

Last heading.

Short answer: use c14n (xml.dom.ext.c14n in PyXML), or an XML smart
tree compare function, such as the one that comes with 4Suite.

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://4Suite.org http://fourthought.com
A hands-on introduction to ISO Schematron -
http://www-106.ibm.com/developerwork...ematron-i.html
Schematron abstract patterns -
http://www.ibm.com/developerworks/xm...y/x-stron.html
Wrestling HTML (using Python) -
http://www.xml.com/pub/a/2004/09/08/pyxml.html
XML's growing pains - http://www.adtmag.com/article.asp?id=10196
XMLOpen and more XML Hacks -
http://www.ibm.com/developerworks/xm...x-think27.html
A survey of XML standards -
http://www-106.ibm.com/developerwork...rary/x-stand4/
Jul 18 '05 #4

P: n/a
Diez B. Roggisch <de*********@web.de> wrote:
The nodeName is just one property you can check for equality - maybe you
want to additionally compare nodeType and nodeValue.
Probably. Also each of Element.attributes if there are any attributes
in the object, and Node.namespaceURI if the document uses namespaces.

Skip Montanaro <sk**@pobox.com> wrote:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails
d1 == d2

False


DOM Level 3 Core defines the method Node.isEqualNode:

http://www.w3.org/TR/DOM-Level-3-Cor...e3-isEqualNode

Which would do such a comparison. minidom doesn't support it yet, but
pxdom does. Otherwise, it's not too much work to write a recursive
comparator such as the above.

(The Python == operator does the same as DOM3's Node.isSameNode, or
'is'.)

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #5

P: n/a
On 8 Nov 2004 10:33:24 -0800, rumours say that sk**@pobox.com (Skip
Montanaro) might have written:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails:
import xml.dom.minidom
d1 = xml.dom.minidom.parse("ES.xml")
d2 = xml.dom.minidom.parse("ES.xml")
d1 == d2

False

My goal is to decide whether or not I need to prompt the user to save config
information at the end of a program run by generating a minidom object then
comparing it with the last saved version.


Convert your minidom objects into sets of ("key", "value") tuples, where
"key" represents the full path to the key starting from the root
element. I assume that both "key" and "value" will or can be hashable.
Compare the two sets.
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.