My current task involves attempting to insert an element into an XML file. Upon inspecting the program with a debugger, I noticed that the element is successfully added to the XML file. However, when I stop the program from running, the changes are not saved to the file. Here is the JavaScript code snippet:
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) {
newMessageElement = xmlDoc.createElement("message");
newTextElement = xmlDoc.createElement("text");
newText = xmlDoc.createTextNode("I am fine");
newTextElement.appendChild(newText);
newMessageElement.appendChild(newTextElement);
x = xmlDoc.documentElement;
x.appendChild(newMessageElement);
}
}
function AddXMLElement() {
xmlhttp.open("POST", "Default.aspx", true);
xmlhttp.setRequestHeader("Accept", "text/xml");
xmlhttp.onreadystatechange = CreateXmlElement;
xmlhttp.send(xmlDoc);
}
Below is the content of the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<conversation>
<message>
<text>Hi</text>
</message>
<message>
<text>How are you?</text>
</message>
</conversation>
Additionally:
I have familiarity with
asp.net
, but not jQuery orphp
.If I modify the open URL to "XMLFile.xml," an error message stating "method not allowed" appears.
A button triggers the execution of the
AddXMLElement()
function.