tags:

views:

188

answers:

2
//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 all your help!

+1  A: 

children is an object in IE6. Perhaps there's an inconsistency in that IE's first child is a text node, whereas in other browsers the first child is a DOM element node? Usually you'd use .childNodes and .childNodes.length and check for .nodeType==1 in a loop to run through the children.

meder
thanks! i'll give that a try.
BeachRunnerJoe
+3  A: 

In IE, an XML document does not implement the same document object model as an HTML document; in particular, XML Node objects don't have the children property, which is non-standard.

You should use the childNodes collection instead. However be aware that in Firefox and other browsers - and, IIRC, under very specific circumstances in IE - this collection will also include text nodes that contain only whitespace, such as line breaks in the original XML file. So you will need to check the nodeType property: if it has the value 1, it is an Element, and will have properties such as tagName.

Furthermore, as MSXML implements DOM Level 1, whereas Firefox implements DOM Level 3, you won't be able to use the textContent property, which was introduced in Level 3. Instead, you will have to iterate over the childNodes of nodeType === 3 and concatenate their nodeValue properties, and probably then will want to trim any leading or trailing whitespace. Alternatively, if you know that there will only ever be textNodes in there, call the normalize method of the element first to make sure it only has one text node child.

Nobody ever said this stuff was supposed to be easy :-(

NickFitz
wow, thanks for breaking it down for me!
BeachRunnerJoe