从Chrome中的xml源读取文档

问题描述:

我在阅读Chrome中的以下XML文档时出现问题 - 没有任何东西出现,但它在Firefox中完美运行。这是浏览器兼容性问题吗?从Chrome中的xml源读取文档

<html> 
    <head> 
    <title>XML</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 
    <script type='text/javascript'> 

    function XMLload(){ 
     jQuery.post(url,function(data){getxml(data);},'xml'); 
    } 

    function dataFromTag(node,t){ 
     var d=node.getElementsByTagName(t); 
     if(d.length==0) 
      return(''); 
     return(d[0].firstChild.nodeValue); 
    } 

    jQuery(document).ready(function(){XMLload();}); 

    // all the code above this line can be re-used without change 
    // all the code below will need altering for a different document and different structure 

    var url='cohort.xml'; 
    var xmlcohort; 

    function getxml(xmldoc){ 
     xmlcohort = xmldoc.getElementsByTagName('student'); 

     var hstr = '<html><head><title></title></head><body>'; 
     hstr += '<p>Cohort:</p><form>'; 
     hstr += '<select size="' + xmlcohort.length + '" onclick="parent.student_info(this.selectedIndex);">'; 

     for(var i = 0; i < xmlcohort.length; i++) { 
      hstr += '<option>' + dataFromTag(xmlcohort[i], 'name') + '</option>'; 
     } 
     hstr += '</select></form></body></html>'; 

     with(document.getElementById('cohort').contentDocument) { 
      open(); 
      write(hstr); 
      close(); 
     } 
    } 

    function student_info(idx) { 
     var hstr = '<html><head><title></title></head><body><dl>'; 
     hstr += '<dt>Name</dt><dd>' + dataFromTag(xmlcohort[idx], 'name') + '</dd>'; 
     hstr += '<dt>Module</dt><dd>' + dataFromTag(xmlcohort[idx], 'module') + '</dd>'; 
     hstr += '<dt>Mark</dt><dd>' + dataFromTag(xmlcohort[idx], 'mark') + '</dd>'; 
     hstr += '</dl></body></html>'; 

     with(document.getElementById('student').contentDocument) { 
      open(); 
      write(hstr); 
      close(); 
     } 
    } 

    </script> 
    </head> 
    <body> 
    <iframe id='cohort' height='300' width='200'></iframe> 
    <iframe id='student' height='300' width='200'></iframe> 
    <p>Here is the <a href="cohort.xml">xml data</a></p> 
    </body> 
</html> 

这里是cohort.xml的内容:

<cohort> 
<student> 
<name>Bob</name> 
<module>COA122</module> 
<mark>72</mark> 
</student> 
<student> 
<name>Alice</name> 
<module>COB250</module> 
<mark>84</mark> 
</student> 
<student> 
<name>Kate</name> 
<module>COP180</module> 
<mark>99</mark> 
</student> 
</cohort> 

,当我从我的硬盘驱动器运行它,我得到这个错误。整个页面没有加载 - 它只有当我把它在Web服务器上的工作原理:

XMLHttpRequest cannot load file:///C:/Users/methuselah/Desktop/cohort/cohort.xml. Origin null is not allowed by Access-Control-Allow-Origin. 

这是浏览器的已知问题:http://code.google.com/p/chromium/issues/detail?id=40787

如果这只是用于测试和开发,有是一个Chrome命令行标志,可用于解决此问题:--allow-file-access-from-files。我只是对它进行了测试,它使得您的网页在Chrome中可以在本地工作。

的Windows:chrome.exe --allow-file-access-from-files

的Mac:/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files

的Linux:Linux用户不需要为如何得到的东西从命令行工作指导! ;-)