Comparing two minidom objects

Diez B. Roggisch deetsNOSPAM at
Mon Nov 8 20:29:02 CET 2004

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.

Diez B. Roggisch

More information about the Python-list mailing list