473,408 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 4793
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
> 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.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
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
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
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...
0
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:...
4
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...
1
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
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...
6
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:...
4
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...
19
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 =...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.