0

I need to edit an xml file using javascript. Now I'm involved in a project of online testing.

The question.xml file is already in the project folder.

In that i want to add or edit the questions(only using javascript). I'm able to bring the particular content through ajax, but I am not able to edit the file.

DJ22T
  • 1,628
  • 3
  • 34
  • 66
praveenjayapal
  • 37,683
  • 30
  • 72
  • 72
  • Where does the file come from? Is it uploaded by a user through your web application? Or does it reside on the server? Update your question with the answer. – Benry Dec 04 '08 at 06:17

6 Answers6

5

Javascript can't write to a file. The best you'll be able to do is get Javascript to read and edit the XML then post that data to a server-side script to write to file.

nickf
  • 537,072
  • 198
  • 649
  • 721
  • This was historically true, but browsers are now able to modify local files using the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API). – Anderson Green Feb 15 '22 at 22:57
1

Until now, Google Chrome is the only web browser that has a functioning implementation of the FileSystem API, therefore, it may allow you to save files locally using only Javascript.

Obviously, for security reasons, when writing files to the local file system, the user must explicitly allow it.

A working tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/

dev4life
  • 10,785
  • 6
  • 60
  • 73
0

Nickf is correct. The reason Javascript can't write to a file is because it is a Client-Side language. Javascript will never have permission to write a file because it has to operate inside the browser sandbox.

You will need to use a server-side script (.NET, PHP, ColdFusion, etc) to write the file.

discorax
  • 1,487
  • 4
  • 24
  • 39
0

If you are willing to use Google Gears, you get a sandbox on the client machine on which you can write files.

Yuval F
  • 20,565
  • 5
  • 44
  • 69
  • Google Gears was cancelled in 2011 (3 years after this post). This answer is historically accurate but no longer relevant (with the intro of HTML 5). Although, at the moment, Chrome is the only browser which actually implements the file system api. – JDB Apr 09 '14 at 17:54
0

Javascript has no built-in file I/O (a.k.a. you can't do it with JS alone)

Instead use some kind of server side language such as PHP or ASP.NET in conjunction with Javascript's AJAX functionality.

Andrew G. Johnson
  • 26,603
  • 30
  • 91
  • 135
  • 1
    nitpicking a little bit here, but you're not right about the File IO thing. it does have file INput, just not OUTput. – nickf Dec 04 '08 at 09:51
0

Look over Adobe's Flex development system. There are ways you can use it to build an app that runs in the browser (or not) and can access the filesystem (Windows/Mac/Linux). It's programmed in ActionScript, a dialect of javascript; and can interoperate with javascript in the browser.

dkretz
  • 37,399
  • 13
  • 80
  • 138