Comparing two minidom objects

S

Skip Montanaro

I'd like to compare two xml.dom.minidom objects, but the naive attempt fails:
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
 
D

Diez B. Roggisch

Skip said:
I'd like to compare two xml.dom.minidom objects, but the naive attempt
fails:

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.
 
D

Diez B. Roggisch

res = res and tree_eq(ac, bc)

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

Uche Ogbuji

I'd like to compare two xml.dom.minidom objects, but the naive attempt fails:

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/nodes/2004-11-08/domlette-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/developerworks/edu/x-dw-xschematron-i.html
Schematron abstract patterns -
http://www.ibm.com/developerworks/xml/library/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/xml/library/x-think27.html
A survey of XML standards -
http://www-106.ibm.com/developerworks/xml/library/x-stand4/
 
A

Andrew Clover

Diez B. Roggisch said:
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 said:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails
False

DOM Level 3 Core defines the method Node.isEqualNode:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-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'.)
 
C

Christos TZOTZIOY Georgiou

I'd like to compare two xml.dom.minidom objects, but the naive attempt fails:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top