Sunday, November 04, 2012

Simple XML Processing with DOM and SAX

It's been a while since I last had any need to process XML on the browser. Most of my processing of late has been on the server side which takes care of all the necessary work.

So, this evening, it was nice when I one of the Django application I have been working on required a fair amount of XML processing. In an effort to move some of the load to the client, I have decided that some of the job should only be performed when the require it.

What the application does is that it uses Geo and IP-mapping to detect where the visitor is and then provide them with the appropriate XML. The next stage is to  process that XML. So, to give you a very simple of how that XML is processed, I have included here both the XML and the DOM script for it.
 var doc = null;

function loadXML() {
    doc = document.getElementById("XMLIsland")
}

function DoIt() {
   var output = ""
   var records = doc.documentElement.childNodes;
   for ( var i = 0; i < records.length; i++ ) {
      townName = records.item(i).firstChild.firstChild.nodeValue;
      tempValue = records.item(i).lastChild.firstChild.nodeValue;
      output += "
Temperature in " + townName + " is " + tempValue;
   }
   document.getElementById("output").innerHTML = output;
}
I'm not sure why blogger is deciding to strip out all the formatting in my code above. But will fix it and try to show the code in full. It would be nice to show how you too could move some of your backend processing to the client.

As it turned out, the stripping of HTML tags is also happening o my JRoller page as well - where I originally attempted to post this blog.

Will update it when I find a solution.

No comments:

Related Posts with Thumbnails