在Chrome中加载本地XML文件 [英] Loading local XML file in Chrome

查看:300
本文介绍了在Chrome中加载本地XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用HTML5 / CSS3 / JavaScript制作项目并使用XML作为数据提供者。
要求声明它必须与Internet Explorer,Firefox,Safari和Chrome兼容。
我在Firefox中开发,所以工作得很好。
Safari的工作原理也是如此,幸运的是它在新的IE9中非常完美。



我只在Chrome中运行时遇到问题。
由于Chrome似乎支持许多新的HTML5 / CSS3功能,因此在浏览器中运行它也不应该做太多工作。
唯一的问题是它拒绝加载XML文件,因为这是从开始就需要的,整个事情不会加载。



在做了一些调查之后,我发现一些帖子指出Chrome不允许你加载本地XML文件。
它应该脱机工作而不使用Apache或其他任何东西,那么还有什么办法可以继续使用它吗?



我加载XML的代码片段取决于在使用的浏览器上:

  if(/ MSIE(\ d + \.\\\d +); /。test navigator.userAgent)){
// INTERNET EXPLORER
xmlDoc = new ActiveXObject(Microsoft.XMLDOM);
尝试{
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
//载入xml文件
xmlDoc = loadXMLDoc(exercise1.xml);
} else if(/Chrome[/\\/\s](\d+\.\d+)/。test(navigator.userAgent)){
// GOOGLE CHROME
//载入xml文件
// ????????
} else if(/Opera[\/\s](\d+\.\d+)/。test(navigator.userAgent)){
// OPERA
警报(现在不支持Opera);
} else if(browser.toLowerCase()。indexOf('safari')> 0){
// APPLE SAFARI
//载入xml文件
xmlDoc = loadXMLDoc( exercise1.xml);
} else {
//其他浏览器
//载入xml文件
xmlDoc = loadXMLDoc(exercise1.xml);
}

Firefox和Safari使用外部JavaScript加载XML。
我不认为真的有必要在这里发布代码,因为它在这些浏览器中工作得很好。



也许我忽略了一些简单的东西,但我已经做了一些谷歌搜索,并尝试了一些在Stackoverflow上找到的代码,我仍然无法得到它的工作..

解决方案您可以使用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;
}

这是Chrome开发工具中显示的xmlDoc。





确保服务器上有一个有效的XML文档。

 <?xml version ='1.0'?> ; 

必须位于XML的第一行,并检查标签前是否有空格。


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.

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.

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?

The snippet of code where I load my XML depending on the browser being used:

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.

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..

解决方案

You can load an XML file in Chrome using XMLHttpRequest like this: (tested on 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.

Be sure to have a valid XML document on the server.

<?xml version='1.0'?>

must be on first line of XML and also check that there are no spaces before the tag.

这篇关于在Chrome中加载本地XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆