XML DOM appendChild() ਮੇਥਡ

ਪਰਿਭਾਸ਼ਾ ਅਤੇ ਵਰਤੋਂ

appendChild() ਇਹ ਮੇਥਡ ਨਿਰਦਿਸ਼ਟ ਐਲੀਮੈਂਟ ਨੋਡ ਦੇ ਆਖਰੀ ਉਪ ਨੋਡ ਦੇ ਬਾਅਦ ਨੋਡ ਜੋੜਦਾ ਹੈ。

ਇਹ ਮੇਥਡ ਨਵਾਂ ਉਪ ਨੋਡ ਵਾਪਸ ਦਿੰਦਾ ਹੈ。

ਗਰਿੱਖ

appendChild(node)
ਪੈਰਾਮੀਟਰ ਵਰਣਨ
node ਲਾਜ਼ਮੀ। ਜੋੜਨੀ ਹੋਣ ਵਾਲੀ ਨੋਡ

ਇੰਸਟੈਂਸ

ਉਦਾਹਰਣ 1

ਹੇਠਲਾ ਕੋਡ "books.xml" ਨੂੰ xmlDoc ਵਿੱਚ ਲੋਡ ਕਰਦਾ ਹੈ, ਅਤੇ ਇੱਕ ਨੋਡ (<edition>) ਬਣਾਉਂਦਾ ਹੈ, ਫਿਰ ਉਸਨੂੰ ਪਹਿਲੇ <book> ਨੋਡ ਦੇ ਆਖਰੀ ਉਪ ਨੋਡ ਦੇ ਬਾਅਦ ਜੋੜਦਾ ਹੈ:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var newel = xmlDoc.createElement("edition");
    var x = xmlDoc.getElementsByTagName("book")[0];
    x.appendChild(newel);
    document.getElementById("demo").innerHTML =
    x.getElementsByTagName("edition")[0].nodeName;
}

ਆਪਣੇ ਆਪ ਦੋਹਰਾਓ

ਉਦਾਹਰਣ 2

ਹੇਠ ਲਿਖੇ ਕੋਡ "books.xml" ਨੂੰ xmlDoc ਵਿੱਚ ਲੋਡ ਕਰਦਾ ਹੈ ਅਤੇ ਨਵੇਂ ਨੋਡ ਸਾਰੇ <book> ਐਲੀਮੈਂਟਾਂ ਨੂੰ ਜੋੜਦਾ ਹੈ:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, y, z, i, newel, newtext, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    for (i = 0; i < x.length; i++) {
        newel = xmlDoc.createElement("edition");
        newtext = xmlDoc.createTextNode("first");
        newel.appendChild(newtext);
        x[i].appendChild(newel);
    }
    // 输出所有 title 和 edition
    y = xmlDoc.getElementsByTagName("title");
    z = xmlDoc.getElementsByTagName("edition");
    for (i = 0; i < y.length; i++) {
        txt += y[i].childNodes[0].nodeValue +
        " - Edition: " +
        z[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

ਆਪਣੇ ਆਪ ਦੋਹਰਾਓ