Closed Thread Icon

Topic awaiting preservation: Comparing XML Elements Pages that link to <a href="https://ozoneasylum.com/backlink?for=25883" title="Pages that link to Topic awaiting preservation: Comparing XML Elements" rel="nofollow" >Topic awaiting preservation: Comparing XML Elements\

 
Author Thread
bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-28-2005 06:25

Hey I'm getting back to some JS roots and playing with some Ajax type stuff.

Basically I'm parsing some XML from an API ( no server side access at this point) and I'm grabbing new XML on a fairly quick basis.

What I want to know is if there's a way to compare my current XML document against the new one before I parse it.

Right now my app tends to be a bit clunky so I would like to skip the parsing steps if nothing has changed.

I tried the xml.toString() method hoping that would do something for me but it didn't work.



.:[ Never resist a perfect moment ]:.

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 05-28-2005 15:45

Well, the best way to compare two XML elements is recursively. The following algorithm isn't specific to XML nodes at all, just a general algorithm for testing two trees...

code:
function checkTree (node1, node2) {
   if(node1.value != node2.value)
      return false;

   else
      for(var i in node1.children)
         if(!checkTree(node1[i], node2[i]))
            return false;

   return true;
}



Basically, you check the current two nodes. If they're equal, you check everything below them too. If they're not, then you know the two trees aren't equal. It's been a while since I've played with the XML DOM, but it should be easy enough to tailor this... you basically want to check the nodeType of each node (as the value). If they're text nodes, you'll want to check their contents, too.

Hope that helps.

---
Website

(Edited by Iron Wallaby on 05-28-2005 15:46)

« BackwardsOnwards »

Show Forum Drop Down Menu