Html – Opening a local XML file in Google Chrome

google-chromehtmlxmlxslt

Our tool will look like below

  1. Input an XML file.
  2. Click Load button will do transformation of the XML file and display it in a table format.

Currently the tool is working fine in IE and Firefox. Now we have the requirement to support it for Chrome also.

Inside html file JavaScript, we are doing the XML transformation using the XSLT file. For the purpose of loading XML we are creating a DOM object for different browsers as below.

if(window.ActiveXObject) {
       XMLObject = new ActiveXObject('Microsoft.FreeThreadedXMLDOM');
}

else {
       XMLObject = document.implementation.createDocument("", "", null); 
}

But for Chrome we are not able to find the Compatible DOM object code. While searching in web found that the below code will work in Chrome.

XMLObject =new XMLHttpRequest();

Tried with the above method , but it is throwing the error “Cross origin requests are only supported for HTTP”.

Also while loading XML file found that the browser is not giving the correct path of the filename, instead its giving like C:\fakepath\Sample.xml.

Any other way to solve this issue?

Best Solution

If you want to use XMLHttpRequest to access files from the local file system then you have to start Chrome with the command line option --allow-file-access-from-files.