responseXML different in FF than IE

In a previous thread I got some solid help with js for xml (props to TommiChi). I got it working for IE then found a problem in FF.

As you can see from the alerts below there are differences in the returns by IE and FF for both responseXML (theResponse) and responseXML.documentElement (theRoot). Apparently FF includes the text nodes of each tag node in its responseXML.documentElement.childNodes.length, so it more than doubles the length value over that returned by IE.

I wrote a workaround to test for the ‘#’ in theRoot.childNodes[0].nodeName, but I’m not confident that it’s going to work in all situations.

Can this difference in responseXML returns be mitigated by using a different way of calling XMLHttpRequest() or using a different method altogether?


alert(theResponse.childNodes.length);  //ie= 2  ff= 1
alert(theRoot.childNodes.length);      //ie= 10 ff= 21
alert(theRoot.childNodes[0].nodeName); //ie= review ff= #text 

Here’s the js:


function showReviews() {
  var ajaxGet;
  try {
    ajaxGet = new XMLHttpRequest();
    ajaxGet.open("GET", "reviewtest.xml", true);
    ajaxGet.setRequestHeader("Content-Type", "text/xml");
  }
  catch (err) {
    ajaxGet = new ActiveXObject("Microsoft.XMLHTTP");
    ajaxGet.open("GET", "reviewtest.xml", true);
    ajaxGet.setRequestHeader("Content-Type", "text/xml");
  }
	
  ajaxGet.onreadystatechange = function() {
    if (ajaxGet.readyState == 4) {
      var theResponse = ajaxGet.responseXML;
      var theRoot = theResponse.documentElement; // get the root element
      var tempYears = new Array;

      if(theRoot.childNodes[0].nodeName.substr(0,1) == '#'){
        alert('firefox');
      } else {
        alert('ie');
      }

      for(i = 0; i < theRoot.childNodes.length; i++){
        tempYears[i] = theRoot.getElementsByTagName("reviewyear")[i].childNodes[0].nodeValue;
      }
    }
  }
  ajaxGet.send(null);
}

Here’s the xml:


<?xml version="1.0" encoding="UTF-8"?>
<archive>
<review>
<review_id>1</review_id>
<filmtitle>Test</filmtitle>
<director>Director 1</director>
<producer>Producer 1</producer>
<filmyear>2001</filmyear>
<reviewtitle>Review Title 1</reviewtitle>
<caption>Review Caption 1</caption>
<reviewday>15</reviewday>
<reviewmonth>1</reviewmonth>
<reviewyear>2009</reviewyear>
</review>
<review>
<review_id>2</review_id>
<filmtitle>Test 2</filmtitle>
<director>Director 2</director>
<producer>Producer 2</producer>
<filmyear>2002</filmyear>
<reviewtitle>Review Title 2</reviewtitle>
<caption>Review Caption 2</caption>
<reviewday>16</reviewday>
<reviewmonth>2</reviewmonth>
<reviewyear>2009</reviewyear>
</review>
</archive>

Check Node.nodeType.

Thanks for the help. That’s exactly what I needed.