I’ve attempted to follow several tutorials but have run into a problem when trying to read an XML file using Javascript. Below is some general information about the problem:
Directory
Html file that includes the javascript file is located in root (/).
Javascript file containing all of the javascript seen below is (/js
XML file being called is in /js/views/viewDirectory.xml
var installedViews = createXMLDoc('js/views/viewDirectory.xml');
//alert(installedViews.documentElement.childNodes.length); //Prints '9'
//alert(installedViews.documentElement.tagName); //Prints views
for (var i = 0; i < installedViews.documentElement.childNodes.length; i++){
var viewsNode = installedViews.documentElement.childNodes;
//alert( viewsNode[i].tagName+","+ viewsNode[i].childNodes[1].tagName+","+viewsNode[i].childNodes[1].text); //Prints "View,file,undefined"
//alert( viewsNode[i].tagName+","+ viewsNode[i].childNodes[0].tagName+","+viewsNode[i].childNodes[0].text); //Prints "View,undefined,undefined"
}
function createXMLDoc(file)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST",file,false);
xmlhttp.send();
return xmlhttp.responseXML;
}
You will notice above I’ve included some alert commands with their output.
<?xml version="1.0" encoding="UTF-8"?>
<views>
<view>
<file>CondtionView.js</file>
<class>ConditionView</class>
</view>
<view>
<file>CondtionView1.js</file>
<class>ConditionView1</class>
</view>
<view>
<file>CondtionView2.js</file>
<class>ConditionView2</class>
</view>
<view>
<file>CondtionView3.js</file>
<class>ConditionView3</class>
</view>
</views>
Errors:
Above you will see that there are 4 child nodes. But for some reason the script says this are 9. When looping through the alerts fail every other time because every other child is not a real child. This throws an error in firefox.
In IE i’m getting an access is denied error. However, I’ve printed out xmlhttp.responseText in createXMLDoc() and it prints the xml file. So to me it seems to be able to open the document. In addition, when I print xmlhttp.responseXML it states that it is an XML Doc.
Any suggestions on what could be making this simple task so difficult for me?
Thanks.