Saving to an XML file in Javascript

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
So I'm trying to create a feature of a tool to save to file so that it can be loaded. I'm not sure exactly how to do this. I want it to save to a file on the users computer (this is local only, I recognize the danger of saving to a user's computer with or without their consent) in an XML format. Here's the html I have so far.

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <div id="people">
            <input type="button" value="John Doe" first_name="John" last_name="Doe" />
            <input type="button" value="Jane Doe" first_name="Jane" last_name="Doe" />
        </div>
        <input type="button" value="Save People" onclick="saveToFile()" />
    </body>
</html>

And I want to save first name and last name to a file called people.xml.

Thanks in advance.
 

Skizzerz

Contributors
Staff member
Contributors
Messages
2,929
Reaction score
118
Points
63
javascript in browsers cannot write to files, this is a security feature and one you should not be looking to work around.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Is there any particular reason why it ought to be an XML file? There's a localStorage API now for persistent storage of key-value pairs. Opera seems to be lagging in support (and who the hell knows about Safari?) but it should work in Chrome and FF (and in IE10). (And if the data are small enough, you can always use cookies.)

And the there are proposals (and test implementations) to allow limited filesystem access in JS. Like the localStorage API, sites would have sandboxed access (granted explicitly). It may be able to save back to files explicitly opened by the user (with restrictions similar to the file upload control — no programmatic write access to the file picker). It's sort of necessary to web-centric operating systems (ChromeOS being the best-known current example), especially if you want to allow for cross-OS "true" web applications. The current work-around is an upload/download affair, where every operation on a file involves uploading the file and the operation parameters to the server, letting it process on the server, then downloading the result.
 

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
I prefer using XML because it's easy to write/read, but I'm flexible. Ok then, can I use like an AJAX request to a PHP page to write to a file? I can understand saving one record at a time but dynamically through post parameters is the only way I know of and that probably won't work.
 
Top