简体   繁体   English

在IE8中使用xmlhttp加载XML文件

[英]Loading an XML file using xmlhttp in IE8

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. 我正在设计一个在线调查,其中一个主要功能是将问题外部存储在XML文件中,每次都会加载随机问题。 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: 虽然我在Firefox中运行的代码很好,但当我尝试在Internet Explorer 8中加载页面时,我收到“Access is denied”错误。我已将问题隔离到以下代码部分:

//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. 特别是,错误似乎是在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. 我在其他几个网站上查找了类似的问题,他们似乎暗示有某种域错误触发了IE8的安全设置。 Is this the case, or is there more to it than that? 是这种情况,还是还有更多呢?

Thanks for your help. 谢谢你的帮助。

on IE you can use XPATH directly on xml doc, other browsers do it by creating a xpath parser 在IE上,您可以直接在xml doc上使用XPATH,其他浏览器通过创建xpath解析器来实现

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: 但是一旦你有了[0]索引选择器,就可以更有效地执行以下操作:

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) selectNodes将返回一个节点列表(列表不是dom部分)selectSingleNode将返回第一个节点(不是列表)

however you can do the final selection in one command with xpath like: 但是您可以使用xpath在一个命令中进行最终选择,如:

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 / * [n]表示法是可以的,但如果它们是唯一的或两者的组合,则可以使用节点名称

having the node you can add .noValue, .text, .textContent, .nodeName as required 拥有可以根据需要添加.noValue,.text,.textContent,.nodeName的节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM