在 Chrome 中加载本地 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5578979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading local XML file in Chrome
提问by Jort
I have to make a project for work using HTML5/CSS3/JavaScript, and using XML as the dataprovider. The requirements state that it has to be compatible with Internet Explorer, Firefox, Safari and Chrome. I'm developing in Firefox, so that works just fine. Safari works like a treat as well, and luckily it works perfect in the new IE9.
我必须使用 HTML5/CSS3/JavaScript 制作一个工作项目,并使用 XML 作为数据提供者。要求规定它必须与 Internet Explorer、Firefox、Safari 和 Chrome 兼容。我正在 Firefox 中开发,所以效果很好。Safari 也可以作为一种享受,幸运的是它在新的 IE9 中运行完美。
I'm only having problems getting it to run in Chrome. As Chrome seems to support a lot of the new HTML5/CSS3 features, it shouldn't be too much work to get it running in that browser as well. The only problem is that it refuses to load the XML file, and since this is required from the get-go, the entire thing won't load.
我只是在让它在 Chrome 中运行时遇到问题。由于 Chrome 似乎支持许多新的 HTML5/CSS3 功能,因此在该浏览器中运行它应该不会有太多工作。唯一的问题是它拒绝加载 XML 文件,并且因为从一开始就需要这样做,所以不会加载整个文件。
After doing some research, I came across posts stating that Chrome does not allow you to load local XML files. It should work offline without using Apache or anything, so is there any way to still get it working?
在做了一些研究之后,我发现一些帖子指出 Chrome 不允许您加载本地 XML 文件。它应该在不使用 Apache 或任何东西的情况下脱机工作,那么有什么办法让它继续工作?
The snippet of code where I load my XML depending on the browser being used:
我根据所使用的浏览器加载 XML 的代码片段:
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
// INTERNET EXPLORER
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
try {
xmlDoc.async="true";
xmlDoc.load("exercise1.xml");
} catch(ex) {
alert("exception");
alert(ex.message);
}
}else if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
// MOZILLA FIREFOX
// load xml file
xmlDoc=loadXMLDoc("exercise1.xml");
}else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
// GOOGLE CHROME
// load xml file
// ????????
}else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
// OPERA
alert("Opera is not supported as of now");
}else if (browser.toLowerCase().indexOf('safari') > 0){
// APPLE SAFARI
// load xml file
xmlDoc=loadXMLDoc("exercise1.xml");
} else {
// OTHER BROWSERS
// load xml file
xmlDoc=loadXMLDoc("exercise1.xml");
}
Firefox and Safari use an external JavaScript to load the XML. I don't think it's really necessary to post that code here, since it works perfectly fine in those browsers.
Firefox 和 Safari 使用外部 JavaScript 加载 XML。我认为没有必要在此处发布该代码,因为它在这些浏览器中运行良好。
Perhaps I've overlooked something simple, but I've done a fair bit of googling and tried some code found here on Stackoverflow, and I still can't get it to work..
也许我忽略了一些简单的事情,但我已经做了一些谷歌搜索并尝试了一些在 Stackoverflow 上找到的代码,但我仍然无法让它工作..
回答by R?zvan Flavius Panda
You can load an XML file in Chrome using XMLHttpRequest like this: (tested on Chrome 12.0.742.112).
您可以使用 XMLHttpRequest 在 Chrome 中加载 XML 文件,如下所示:(在 Chrome 12.0.742.112 上测试)。
function readxml()
{
xmlHttp = new window.XMLHttpRequest();
xmlHttp.open("GET","test.xml",false);
xmlHttp.send(null);
xmlDoc = xmlHttp.responseXML.documentElement;
}
This is the resulting xmlDoc seen in Chrome development tools.
这是在 Chrome 开发工具中看到的结果 xmlDoc。


Be sure to have a valid XML document on the server.
确保服务器上有一个有效的 XML 文档。
<?xml version='1.0'?>
must be on first line of XML and also check that there are no spaces before the tag.
必须在 XML 的第一行,并检查标签前是否没有空格。

