Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

And now in Firefox - XML parsing.

So far I have created two apps that will parse a given XML file in javascript - Binny's Software DataBase and JASFER. Both these programs had the same crippling problem - neither worked in Firefox. The work very well in IE - but in firefox, they have this burning desire to crash and burn.

But not anymore - now both are working in both IE and Firefox. I don't know about the other browsers - but generally, if any page works on the above two browsers it passes my minimum requirements.

Some links that helped me solve the problem...

XML DOM parser Javascript RSS reader

The problem was that I was using absolute item reference - like this...

nodes = data.documentElement.childNodes.item(0).childNodes
alert("Length : " + nodes.length
  + "\nFirst Item : " + nodes.item(0).nodeName
  + "\nFirst Item Value : " + nodes.item(0).childNodes[0].text);
What I should have used is the getElementsByTagName() function - like this...
nodes = xmlDoc.getElementsByTagName("item"); //Get all the 'item's
alert("Length : " + nodes.length
  + "\nFirst Item : " + nodes[0].firstChild.nodeName
  + "\nFirst Item Value : " + nodes[0].firstChild.nodeValue);

I have created a small cross browser framework for loading XML file while creating these softwares. The code is given below. If you have any use for it, feel free to use it - release in GPL licence.

////////////////////////////// XML File Loading ///////////////////////////////
//Firefox only function - happens when a XML file is loaded
function loadHandler () {
  xmlProcessor(this); //Call the Commen function with 'this' data.
}

//Load the xml file - using different method for different browsers
function xmlLoad(xml_file) {
  //Initializations
  feed_id = 0;
  feed_total = 0;

  var xmlDocument = "";
  feed_file = xml_file;
  if(document.implementation.createDocument) {//Firefox
      xmlDocument = document.implementation.createDocument('', '', null);
      xmlDocument.load(xml_file);
 //This function will happen when the file is loaded
 xmlDocument.addEventListener('load', loadHandler, false);
  }
  else { //IE
      var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
      xmlDocument.async = false;
      var loadResult = xmlDocument.load(xml_file);
      if (loadResult) {
          // process xml document with DOM methods e.g.
          xmlProcessor(xmlDocument)
      } else {
          xmlError();
          return false;
      }
  }
  return true;
}

function xmlError() {
  alert("Some error has occurred!")
}

//Process the xml data
function xmlProcessor(xmlDoc) {
  if(!xmlDoc) {
      feedError();
  }

  //Do whatever you want with the XML data...
  nodes = xmlDoc.getElementsByTagName("item"); //Get all the 'item's
  alert("Length : " + nodes.length
  + "\nFirst Item : " + nodes[0].firstChild.nodeName
  + "\nFirst Item Value : " + nodes[0].firstChild.nodeValue);

}

xmlLoad("file.xml"); // Load the XML file.

2 Comments:

Anonymous said...

what does your file.xml look like?

Roman Gherman said...

Hi,

Cross Browser XML Parsing and functions on this article from my blog:
http://extremedev.blogspot.com/2011/03/xml-parsing-and-other-xml-utilities.html

This is working on all major browsers and have some addition functions.

You can find me on http://extremedev.blogspot.com