3

I'm designing an online survey, with one of the major features being that the questions are stored externally in an XML file, with random questions being loaded in each time. While the code I have works fine in Firefox, I get "Access is denied" errors when I try to load the page in Internet Explorer 8. I've isolated the problem to the following portion of code:

//Import the XML File
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5*/
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

//Import XML
xmlhttp.open("POST","Personalized Tour/questions.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

In particular, the error seems to be thrown on xmlhttp.open. I looked on several other websites for similar problems, and they seemed to suggest there was some kind of domain error that was triggering IE8's security settings. Is this the case, or is there more to it than that?

Thanks for your help.

Robert
  • 31
  • 2
  • Are you using "file://" URLs? If so that's probably the issue - IE doesn't think that "file://" pages share a domain, so it protects them from eachother. Chrome does the same thing. – Pointy Mar 24 '11 at 16:01
  • `"Personalized Tour/questions.xml"` is not a valid URL. No spaces allowed. – Tomalak Mar 24 '11 at 16:07
  • Thanks. The issue, was, in fact, the "file://" URL, and it looks like that problem is fixed now. – Robert Mar 24 '11 at 17:27
  • Unfortunately, I now seem to be having a different problem. It seems to be loading in the XML okay, but my code throws errors when I try to access the data in the xmlDoc variable. Do you know if IE uses different xmldom syntax? The code I have right now: `var majorCats = xmlDoc.getElementsByTagName("major")[0]; var printValue = majorCats.childNodes[1].childNodes[9].childNodes[0].nodeValue;` – Robert Mar 24 '11 at 17:42

1 Answers1

0

on IE you can use XPATH directly on xml doc, other browsers do it by creating a xpath parser

so, selecting all "major" elements would be like:

xmlDoc.selectNodes("//major")

but once you have that [0] index selector you can do it more efficiently with:

xmlDoc.selectSingleNode("//major")

the results of both are quite diferent (beside the performance gain)

selectNodes will return a list of nodes (the list is not a dom part) selectSingleNode will return the first node (not a list)

however you can do the final selection in one command with xpath like:

xmlDoc.selectSingleNode("//major/*[1]/*[9]/*[0]")

/*[n] notation is ok but you can use node names if they are unique or a combination of both

having the node you can add .noValue, .text, .textContent, .nodeName as required

neu-rah
  • 1,662
  • 19
  • 32