Reputation: 4474
I make program to read xml file by using traditional JavaScript.
<script type="text/javascript">
var xmlDoc;
function loadxml(sImportXML) {
if( window.ActiveXObject && /Win/.test(navigator.userAgent) ) {
xmlDoc = new ActiveXObject("Msxml.DOMDocument");
xmlDoc.async = false;
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) readXML();
}
xmlDoc.load(sImportXML);
}
else if( document.implementation && document.implementation.createDocument ) {
xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.async=false;
alert(sImportXML);
var loaded = xmlDoc.load(sImportXML);
if (loaded) {
readXML();
}
}
else {
alert("Your browser can\'t handle this script");
return;
}
}
<body onload="loadxml('../XML/Question.xml');">
Upper loadxml function run correctly at IE but not at firefox.
alert line display this value ../XML/Question.xml.
but xmlDoc.Load function did not run correctly.
It reply error Access to restricted URI denied
Please anyone help me.
Upvotes: 3
Views: 942
Reputation: 53319
In Firefox the javascript will not let you access files on the user's local file system; there's simply no way to do it. That would be a huge security breach.
This works in Internet Explorer with ActiveX because ActiveX is a plug-and-play application module system (kind of like mini browser plugins) where the apps, like 'Msxml.DOMDocument', have more power than just javascript and can access files on the user's local file system.
But document.implementation
is regular javascript, so it has all normal security restrictions, a major one being that the user's file system is off limits.
If the XML file is on the server, you can embed in your html code like this:
<script id="the-xml" type="text/xml">
....your xml document contents here....
</script>
Then you can get the contents in javascript like this:
var sImportXML = document.getElementById('the-xml').text;
But depending on your application, it might make sense to not use the javascript xml at all. Usually, you'd parse the xml on the server side, and communicate with javascript in json or html snippets.
Upvotes: 2