Prachi Verma
Prachi Verma

Reputation: 101

reading local xml from javascript

I want to read a locally stored xml, and I want to read/write through javascript. my code is as follows

function saveBaseValue()
{
  var xmlhttp;
  var XMLname="file:///C:/Users/setting.xml"
  if (window.XMLHttpRequest)
    {
    xmlhttp=new window.XMLHttpRequest();
    xmlhttp.open("GET",XMLname,false);
    alert(xmlhttp.responseXML);
   }

}

In this case, xmlhttp.responseXML is null.

Upvotes: 0

Views: 583

Answers (1)

Deep
Deep

Reputation: 9794

you can achieve your goal by using the below code.

<!DOCTYPE html>
<html>
<head>
<script>
function saveBaseValue()
{
  var xmlhttp;
  var XMLname="file:///C:/Users/setting.xml"  
  if (window.XMLHttpRequest)
  {
   xmlhttp=new window.XMLHttpRequest();
   xmlhttp.onreadystatechange = function() 
   {
      if (xmlhttp.readyState == 4) 
      {    
         alert(xmlhttp.responseXML);
      }
   };
   xmlhttp.open("GET",XMLname,false);
   xmlhttp.send();
  }
 }
 saveBaseValue();
 </script>
 </head>
 <body>    
   <h1>Test</h1>
 </body>
</html>

By default your browser will not allow you to access this local file as this a security concern, however if you want to access it anyhow then close all chrome instance, go to command prompt, change the directory to where your chrome.exe is present and run below command.

.\chrome.exe --allow-file-access-from-files --disable-web-security

Upvotes: 4

Related Questions