473,573 Members | 3,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing two minidom objects

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
5 4813
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.childNode s) == len(b.childNode s):
res = True
for ac, bc in zip(a.childNode s, 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
> 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
sk**@pobox.com (Skip Montanaro) wrote in message news:<72******* *************** ****@posting.go ogle.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.c1 4n 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
Diez B. Roggisch <de*********@we b.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.attribu tes if there are any attributes
in the object, and Node.namespaceU RI 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.isEqualNod e:

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*@doxd esk.com
http://www.doxdesk.com/
Jul 18 '05 #5
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7488
by: xtian | last post by:
Hi - I'm doing some data conversion with minidom (turning a csv file into a specific xml format), and I've hit a couple of small problems. 1: The output format has a header with some xml that looks something like this: <item xmlns="" xmlns:thing="http://www.blah.com"> <thing:child name="smith"/> </item>
0
1439
by: Oliver Walczak | last post by:
Can i call normalize to the dom root node so that all adjacent child text nodes attached to one of the element nodes are joined? -----Ursprüngliche Nachricht----- Von: python-list-bounces+oliver.walczak=momatec.de@python.org Im Auftrag von Fredrik Lundh Gesendet: Freitag, 28. November 2003 12:14 An: python-list@python.org Betreff: Re:...
4
5304
by: Skip Montanaro | last post by:
I'm getting somewhat painfully acquainted with xml.dom.minidom. What is the relationship between its documentElement attribute and its childNodes list? I thought XML documents consisted of a single, possibly compound, node. Why is a list of childNodes needed? Thx, Skip
1
4888
by: Ruslan | last post by:
Hi, everybody. In this excerpt of code enc = 'some_type_of_encoding' def _encode(v): if isinstance(v, UnicodeType): v = v.encode(v) return v
5
6361
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some reason, though, it just doesn't seem to want to split the prefixes from the rest of the tags when parsing. The minidom documentation at...
6
3970
by: Horst Gutmann | last post by:
Hi :-) I currently have quite a big problem with minidom and special chars (for example &uuml;) in HTML. Let's say I have following input file: -------------------------------------------------- <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
4
6051
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using...
19
2635
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor = Color.Empty then..... or if mycolor is Color.Empty then .......
0
1509
by: Gary | last post by:
Howdy I ran into a difference between Python on Windows XP and Linux Fedora 6. Writing a dom to xml with minidom works on Linux. It gives an error on XP if there is an empty namespace. The problem was handled in CVS a while ago. http://mail.python.org/pipermail/xml-sig/2003-October/009904.html
0
7777
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7686
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8016
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7773
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5289
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2197
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.