0

I am trying to add an entry to a xml file using javascript. The code below is supposed to add a node called book to this file. But it simply doesn't work. I have also tried some other code just to change an entry in the xml database, but also without success. So what is my fault?

CODE:

function loadXMLDoc(dname) {
if (window.XMLHttpRequest) {
    xhttp=new XMLHttpRequest();
}
else {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}

xmlDoc=loadXMLDoc("database.xml");

newNode = xmlDoc.createElement("entry");
newNode.nodeValue = "aaaaa";
x=xmlDoc.documentElement;
x.appendChild(newNode);

XML FILE (database.xml):

<?xml version="1.0" encoding="ISO-8859-1"?>
<database>
<entry>
    <title>Everyday Italian</title>
    <content>Strange. I seem to get hungry about the same time every day!</content>
    <time>August 7, 2012, 6:24 PM</time>
    <comment>Giada De Laurentiis</comment>
</entry>
<entry>
    <title>I'm Hungry</title>
    <content>I really need something to eat!!</content>
    <time>August 7, 2012, 6:24 PM</time>
    <comment>Giada De Laurentiis</comment>
</entry>
</database>

1 Answer 1

1

You are reading an XML file through the network, and I am guessing that you ARE modifying the XML document which is in MEMORY by adding a new node. But there is nothing in your code that would save the modified XML file from memory to a persistable medium. You could implement a POST or PUT method to write the file just as you implemented the GET method to read it. Of course, your web server should be configured to take such a PUT request and overwrite the original file.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you please give me an example on how to implement this POST OR PUT method??
That would depend entirely on which software you are using as a web server. Are you using IIS? or Apache? or NodeJS?
Fixed the whole thing with php. What a dumb idea to modify a file client side ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.