How to read local XML file using xmlhttp
I have an XML file saved on my local machine and I want to read it via javascript. Below is my local xml path D:\user\xml\test1.xml
.
<script>
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","file///D:/user/xml/test1.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc);
</script>
I can't find the answer in the alert. I am new to XML and am learning the code now. Is my specification of system paths correct? How can I check if my request is opening an xml file and reading input?
source to share
First of all, you did file///...
instead file:///...
so that this could be one of your problems.
But more importantly, I found that it is not possible to xmlhttprequest a local file with javascript because that would be a serious security risk The only solution I can think of for you is to write a server with nodes or ruby ββon rails or what something else, and ask the server to read the file and create a page on that server with the contents of this file. Then you can send an xmlhttp request to this page .
EDIT: I forgot there is a File API in HTML5 (thanks to Zack for pointing this out). You can use this to read an XML file and then parse it. See this page for how to parse xml.
source to share