View Full Version : Parsing *local* XML files in Javascript...Possible?

02-25-2007, 05:22 PM
Hi... I've been pulling my hair out here and I just want a simple answer.

I want to use Javascript to parse an XML Document and grab a simple variable out of there to throw into a form input. The catch is that it's a LOCAL XML file, and I've been getting "Permission Denied" errors.

Basically before I rip ALL my hair out I just want to know if it's even POSSIBLE to parse local XML files using client-side scripting or if I should just give up and go with server-side?

It doesn't REALLY matter I guess, but I was hoping I could keep it all client side...

02-25-2007, 05:29 PM
Just to be more specific, the exact line of code that's giving me problems is (yes I'm using Firefox)...:

xmlDoc.async = false;

I just want a single variable out of an xml file that the user chooses to send me! I'm not going to do anything bad, honest!! :)

02-25-2007, 05:33 PM
Imagine you go to a webpage and it is capable of reading your local files........

02-26-2007, 12:26 AM
Yes yes yes... I understand all that...

But the thing is this isn't ANY local file... it's one that the user VOLUNTARILY INPUTS... this is the original REASON for the necessity of a type="file" input, no?

02-26-2007, 12:50 AM
The only purpose of the type="file" form field is to upload the file to the server. Here is the RFC that defines this - http://www.ietf.org/rfc/rfc1867.txt

02-26-2007, 01:06 AM
So I take it that means "no, you can't do it?"

02-26-2007, 03:12 AM
Actually, if you are using IE you can do it. I wrote this a loooong time ago for when I was working an internal help desk. At the end of each shift, we were required to send a list of the escalated issues we handled, and a brief description of each, to our manager. I created this page to make my life easier. When you launch the page, it looks for a folder on your local machine named, "esc_track". If it does not exist, the script creates it. If it does exist, the script looks through the folder for saved escalation "reports", to find the one for today. If no such file exists, it creates it. If one does exist, it reads the contents of the file and displays it in the text area. I'm sure you could adapt this for what you need. Just a reminder though, it's going to be IE only. As far as I know, this is simply not possible in Firefox.

Link to the file, saved as text:

Just a note, I wrote this a long time ago and the code ain't pretty. I know that there is likely many, many things that could be done to improve it...just never seemed to get around to doing it. :)

02-26-2007, 10:25 AM
Well, if possible I'd like to make a cross-browser solution... not to mention the fact that I wouldn't really care to exploit a security hole just so that I can do something perfectly legitimate! I'm trying server-side solutions to this problem now, but I'm STILL pulling my hair out over security issues.

Perhaps someone would like to suggest an alternate path I could follow based on what I want to do... it REALLY shouldn't be that difficult...

All I want is to have a form in which you can enter (geographic) coordinates. If you know them, you can enter them yourself - if not, then you can upload (or let's say just "input"...depending on whether client-side scripting can be used for this) a Google Earth KML file. The file may as well be a text file...let me worry about the XML. I simply want a way to ACCESS this file so that I can parse it for the precious, precious coordinates inside! What I'm REALLY looking for is an inline solution ... in other words when the user chooses a file, the browser location stays put and the coordinates "pop" into the appropriate field. Sounds like a job for AJAX, but ajax doesn't support file uploading!

I would very much appreciate an ELEGANT solution for this...

02-26-2007, 05:08 PM
Try to look into signed javascript (http://www.google.com/search?hl=en&q=signed+javascript).

02-27-2007, 07:46 AM
Thanks... I'll have a look at this, but I think I'll probably end up going with passing variables back and forth between an iframe.