how to save xml file with ajax and javascript



I am trying to add an element to an xml file. I have checked the program with debugger and I saw it really adds the element to the xml file but when I stop the running the file didn't saved any changes. here is the javascript file:



var xmlhttp = LoadXMLHttp();
var xmlDoc=LoadXMLDoc("XMLFile.xml");;
function LoadXMLHttp() {
var xmlHttp;
if (window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
else
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
function LoadXMLDoc(FileName) {
xmlhttp.open("GET", FileName, false);
xmlhttp.send(null);
return xmlhttp.responseXML;
}
function CreateXmlElement() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
newMassageElement = xmlDoc.createElement("massage");
newTextElement = xmlDoc.createElement("text");
newText = xmlDoc.createTextNode("I am fine");
newTextElement.appendChild(newText);
newMassageElement.appendChild(newTextElement);
x = xmlDoc.documentElement;
x.appendChild(newMassageElement);
}
}
function AddXMLElement() {
xmlhttp.open("POST", "Default.aspx", true);
xmlhttp.setRequestHeader("Accept", "text/xml");
xmlhttp.onreadystatechange = CreateXmlElement;
xmlhttp.send(xmlDoc);
}


And here is the xml file:



<?xml version="1.0" encoding="utf-8" ?>
<conversation>
<massage>
<text>Hi</text>
</massage>
<massage>
<text>How are you?</text>
</massage>
</conversation>


B.T.W:


1) I dont know jquary or php but I do know asp.net


2) If I change the open url to "XMLFile.xml" I get an error massage that says:" method not allowed"


3) I have a button that that actives the AddXMLElement() function


No comments:

Post a Comment