XML DOM appendChild() စနစ်

အသုံးပြုနည်း နှင့် ဖော်ပြ

appendChild() ဒါက အချက်အလက် အစားထိုးထားပြီး အပိုင်းအချက် အစားထိုးထားပါ

ဒါက အသစ်သည် အပိုင်းအချက် ကို ကုန်းမှုးသည်

အပြောအဆ

appendChild(node)
ပါဝင်သည် ဖော်ပြ
node လိုအပ်သောအရာ

အကြောင်း

အမှတ် 1

အောက်ပါ ကြောင်းရာ ကို xmlDoc တွင် "books.xml" ကို တက်ကူးပြီး အက်ကြီး (<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 + "<br>";
        " - Edition: " +
        z[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

亲自试一试