/    Sign up×
Community /Pin to ProfileBookmark

Parse XML file from network share

Hi all,

I have the following situation and I’m hoping that one of you can point me in the right direction.

I have a web page that is not and will not be hosted on a web server. So no PHP, ASP solutions will work here. The method of access is File://. We only run IE 7/8 so no solutions that are firefox only.

Here is what I want to do: Use javascript to build a table by reading a XML file stored in the same folder. When I try to use something like:

[CODE]function readFileHttp(fname, callback) {
xmlhttp = getXmlHttp();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
callback(xmlhttp.responseText);
}
}
xmlhttp.open(“GET”, fname, true);
xmlhttp.send(null);
}

xmlDoc=readFileHttp(“123.xml”);[/CODE]

I get a permission denied error at:

[CODE]xmlhttp.open(“GET”, fname, true);[/CODE]

My understanding is that Javascript does not allow scripts to access local files. My hope is that there is a way around that.

Any ideas?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@noel_groomauthorApr 02.2010 — Forgot to include this:

[CODE]function getXmlHttp() {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp == null) {
alert("Your browser does not support XMLHTTP.");
}
return xmlhttp;
}[/CODE]
Copy linkTweet thisAlerts:
@noel_groomauthorApr 05.2010 — Well, by the lack of response I take it that this is not possible using javascript. Would it be possible with any other language? Or, perhaps I can embed the XML inside of the HTML somehow and have the javascript read that. My main reason for doing this, is to make it easy for folks who are not html or computer literate to update this one file to update the page.
Copy linkTweet thisAlerts:
@rnd_meApr 05.2010 — put your xml files in the same folder as your html files.

you can read files in the same folder as the page, or in a sub-folder of the page's base directory.

so, if your html and xml are on the same share, your code should perform just fine.

if you can't do that, there are some workarounds, especially for IE only.

also, renaming the .html file .hta, will let you by-pass the domain restrictions that are preventing you from using your code as-is.

more info:

http://msdn.microsoft.com/en-us/library/ms536496%28VS.85%29.aspx#Security
Copy linkTweet thisAlerts:
@noel_groomauthorApr 05.2010 — I just verified that my xml file and my html file are in the same directory my desktop. I am an administrator on the local machine. Still no mojo.

Also tried the HTA rename, and it didn't work either. I still received the access denied message.

Is there anything in IE that would be blocking it? For testing purposes I did "allow active content to run in files on My Computer."
Copy linkTweet thisAlerts:
@astupidnameApr 05.2010 — It is do-able with JScript (Microsoft proprietary language, many similarities to - but not javascript):

index.html
[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Local XML</title>
<script type="text/JScript">
/************ NOTE THIS IS JScript, not javascript, IE ONLY ************/
function textToXML(txt) {
var xml = null;
try{
xml = new ActiveXObject("Msxml2.DOMDocument.6.0");
}catch(e){
try{
xml = new ActiveXObject("Msxml.DOMDocument.3.0");
}catch(e){
return xml;
}
}
xml.async = "false";
xml.loadXML(txt);
return xml;
}
function getFileText(path) {
var fso = new ActiveXObject('Scripting.FileSystemObject'),
tf = fso.OpenTextFile(path, 1), //the '1' means ForReading,
//2 would be ForWriting, consult: http://msdn.microsoft.com/en-us/library/czxefwt8(v=VS.85).aspx
s = tf.ReadAll();
return s;
}
/************ SET PATH TO THE LOCAL XML FILE, note backslash escaping *************/
var p = 'C:\testing\Local_XML_JScript\address.xml';
var fileText = getFileText(p);
var xml = textToXML(fileText);
alert(fileText +'n'+ typeof xml);
// xml is now an XML object, do what you will with it...
</head>
<body>
<div>
</div>
</body>
</html>[/code]


For more about scripting the FileSystemObject see the docs at MSDN:

[URL]http://msdn.microsoft.com/en-us/library/bkx696eh(v=VS.85).aspx[/URL]
Copy linkTweet thisAlerts:
@noel_groomauthorApr 06.2010 — Thanks for the help! I appreciate it.
Copy linkTweet thisAlerts:
@astupidnameApr 06.2010 — You're welcome, fix one thing I screwed up (typo) there, this:
"Msxml.DOMDocument.3.0"[/quote]
Should be:
"Msxml[COLOR=red]2[/COLOR].DOMDocument.3.0"[/quote]
×

Success!

Help @noel_groom spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...