-1

I am trying to create a simple XML node with text "New node!"

var xmlDoc = loadXMLDoc("myFile.xml");
var newElem = xmlDoc.createElement("elem");
newElem.innerHTML = "New node!";

Where loadXMLDoc() is

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;
}

But the code does not work. I expect the XML file to have a new node "<elem>" with "New node!" in it, but it was still the same. I have no idea why. There were no error messages.

How do I get my code to work?

chris97ong
  • 6,870
  • 7
  • 32
  • 52

1 Answers1

1

Your code is creating a new element, but you are not appending it to the XML.

See the example here: https://developer.mozilla.org/en-US/docs/Web/API/document.createElement#Example

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Christophe
  • 27,383
  • 28
  • 97
  • 140