0

I know there are several questions how to save changes in an XML file using Javascript, and the obvious answer is: use a server side language such as PHP.

In my case, the XML file is in the same directory as my html file locally on my computer, not on any server.

In my index.html I access the alphabets.xml this way:

function getAlphabets() {
    var xhttp;var result;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            result = findAlphabets(this);
            console.log(result);

        }
    };
    xhttp.open("GET", "alphabets.xml", true);
    xhttp.send();
}

I want to change some data in the xml file, and save it back there. Is there any possible way to do so in plain javascript?

Or another way, not using a server side language?

maxischl
  • 579
  • 1
  • 11
  • 29
  • 2
    JavaScript in the browser can not update files.... Run a local node server, use node (aka javascript) to do the file manipulation. – epascarello Mar 18 '19 at 17:22
  • @epascarello is correct, you can't do that with JS from the browser. You still need a server side language. If you want to use JavaScript, then you would have to be running NodeJS as the server. The only sort of alternative is to write a browser addon that manipulates local files. But that's probably further away from the whole having a page and a server setup. – VLAZ Mar 18 '19 at 17:26
  • You are going to need NodeJS for this one. https://stackoverflow.com/questions/2496710/writing-files-in-node-js – Azael Mar 18 '19 at 17:27
  • 1
    Maybe the following library is of interest to you: https://github.com/eligrey/FileSaver.js/ – Sam Herrmann Mar 18 '19 at 17:32
  • @SamHerrmann is it correct that FileSaver.js requires Node.js? I have some struggles to get it working so far – maxischl Mar 18 '19 at 17:46
  • No it does not. See demo here: https://stackblitz.com/edit/js-f7jaxd?file=index.js – Sam Herrmann Mar 18 '19 at 19:52

0 Answers0