[xmljs-users] Comparing two DOMDocuments for equality?
Brought to you by:
djoham,
witchhunter
From: Stepan R. <xm...@no...> - 2004-10-23 02:31:12
|
Is there an easy way to compare the contents of two W3C DOMDocument objects for equality? Right now I try to do this by comparing (doc1.toString() == doc2.toString()), but that may return false if the order in which attributes were added to nodes in the documents is different (even though the documents are semantically equivalent). It would be nice if there was a compare() method I could call on the DOMDocument or if the toString() methods would produce a canonical form. I'm planning to implement the canonical toString() by changing the DOMNamedNodeMap_toString() method to concatenate the elements sorted by their names. This should produce the same value regardless of the order in which elements were added to the NamedNodeMap. The reason I'm doing this is that in my XML editing web app, I want to warn a user if they leave the page without having saved the changes they made. So on page load, I capture the current XML document and in my window.onbeforeunload handler I compare it with the current (edited) document, and if they differ, display a warning. With the current implementation I sometimes get a warning if the user just "clicks around" because I delete/reinsert attributes in a different order. - Stepan -- Stepan Riha |