Download local XML file in Chrome

I need to create a project to work using HTML5 / CSS3 / JavaScript and use XML as a datapath. The requirements indicate that it must be compatible with Internet Explorer, Firefox, Safari, and Chrome. I am developing in Firefox, so everything works fine. Safari works as a pleasure and, fortunately, it works great in the new IE9.

I only have problems running in Chrome. Since Chrome seems to support many of the new HTML5 / CSS3 features, there should not be too much work to run in this browser. The only problem is that he refuses to download the XML file, and since get-go requires it, the whole thing will not load.

After a series of studies, I came across reports that Chrome does not allow the loading of local XML files. It should work offline without using Apache or anything else, so is there any way to make it work?

A snippet of code in which I load my XML depending on the browser 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 external JavaScript to load XML. I do not think it is really necessary to publish this code here, since it works fine in these browsers.

I might have missed something simple, but I did a small part of the search and tried to find the code found here in Stackoverflow, and I still can't get it to work.

+4
source share
1 answer

You can load the XML file into Chrome using XMLHttpRequest as follows: (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 result of xmlDoc, which can be seen in the Chrome development tools.

readxml () result

You must have a valid XML document on the server.

 <?xml version='1.0'?> 

should be in the first line of XML, and also check for spaces before the tag.

+2
source

Source: https://habr.com/ru/post/1347067/


All Articles