Why does this Javascript DOM code only work in FF but not IE?

//create an instance of the XML parser
if (window.ActiveXObject)
{ 
    //Checking if the browser is IE
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false"; //make sure doc is fully loaded
    xmlDoc.load(strPath) //load the file in the parser
    if (xmlDoc.parseError.errorCode != 0) 
    {
        alert("Error #: " + xmlDoc.parseError.errorCode;        
    }        
}

//for mozilla based browsers
else if (document.implementation && document.implementation.createDocument)       
{
    xmlDoc= document.implementation.createDocument("","doc",null); 
    xmlDoc.async=false; //make sure doc is fully loaded
    loaded = xmlDoc.load(strPath);
    if(!loaded)
    {
       alert("Error in XML File");
    }            
}

//Parse the XML
var root = xmlDoc.documentElement;
level1Nodes = root.children;
for(var index1 = 0; index1 < level1Nodes.length; index1++)
{
    //Extract the markup content from XML
    var level1Node = level1Nodes[index1];
    var strName = level1Node.children[0].textContent;
    var strHeader1 = level1Node.children[1].tagName;
    var strHeader1Content = level1Node.children[1].textContent;
}

      

Any ideas? Is the "children" property available in the IE DOM Parser?

Thanks in advance for your help!

+2


source to share


2 answers


In IE, an XML document does not implement the same document object model as an HTML document; in particular, XML objects Node

do not have a property children

that is non-standard.

You should use a collection instead childNodes

. However, keep in mind that in Firefox and other browsers - and, IIRC, under very specific circumstances in IE - this collection will also contain text nodes that contain only spaces, such as line breaks in the original XML file. So you need to check the property nodeType

: if it has a value of 1, it is an element and will have properties like tagName

.



Also, since MSXML implements DOM Level 1 , whereas Firefox implements DOM Level 3 , you will not be able to use the property textContent

that was introduced in Level 3. Instead, you will have to iterate childNodes

from nodeType

=== 3 and combine their properties nodeValue

and probably then will want to trim any leading or trailing spaces. Also, if you know there will only be text nodes, call the normalize

element method first to make sure it only has one text node.

Nobody ever said that this stuff should be light: --(

+7


source


children

is an object in IE6. Perhaps there is an inconsistency that the first IE is the text node, whereas in other browsers the first child is the DOM node? Usually you use .childNodes

and .childNodes.length

and check for .nodeType==1

in a loop to run through children.



+1


source







All Articles