Question:
Cannot load local XML file using javascript?
dtkdrummer
2010-04-08 15:32:55 UTC
I am using the following code in javascript to attempt to load an XML file so that I can get the values and use them for some logic.
//loadXMLDoc function
function loadXMLDoc(dname) {
if (window.XMLHttpRequest) {
xhttp=new XMLHttpRequest();
}
else {
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

//load xml file
xmlDoc=loadXMLDoc("file://localhost/C:/MapTest/EMSData.xml");
x=xmlDoc.getElementsByTagName("Longitude")[0];
y=x.childNodes[0];
txt=y.nodeValue;
alert(txt);

This is just a test code, and it does not seem to want to load the xml file. The error I get is "Access denied". Am I passing the write path name? Do I need to format it differently? Eventually I want to put this on a site, but right now I'd just like to test it locally on my own comp....any help would be great.

Again the path I'm using is file://localhost/C:/MapTest/EMSData.xml
Three answers:
?
2016-11-03 14:16:28 UTC
Load Xml File Javascript
anonymous
2016-10-15 10:51:40 UTC
To retrieve and walk an xml in basic terms use the xmlreader sort. Create attitude creates a sparkling social accumulating from a record, string, or url that returns XML. There a large form of the variety to stroll/study the xmldocument from there. There distinctive innovations too, yet they are extra advantageous complicated.
J.J.'s Advice / Avis de J.J.
2010-04-08 15:44:14 UTC
Keep in mind that JavaScript is a client-side programming language. You will only be able to access data available to the client. file://localhost is not a valid URL. http://localhost/ exists if you're running a server on your local computer. file:///C:/MapTest/EMSData.xml should work if you're just running this as a file on your local system.

If you plan on making this available to anyone else, you'll have to put the EMSData.xml file in the public directory, and link to it using simply its filename.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...