XML DOM အစားထိုးChild() နည်းလမ်း
❮ Node Object
ဥပမာ
အောက်ပါ ကုဒ်အပိုင်းအစသည် " books.xml " ကို xmlDoc သို့ တင်ပြီး ပထမ <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 x, y, z, i, newNode, newTitle, newText,
xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.documentElement;
// Create a book
element, title element and a text node
newNode = xmlDoc.createElement("book");
newTitle = xmlDoc.createElement("title");
newText = xmlDoc.createTextNode("A
Notebook");
// Add a text node to the title node
newTitle.appendChild(newText);
// Add the title node to the book node
newNode.appendChild(newTitle);
y = xmlDoc.getElementsByTagName("book")[0];
// Replace the first book node with the new book node
x.replaceChild(newNode,
y);
z = xmlDoc.getElementsByTagName("title");
// Output all titles
for (i = 0; i < z.length; i++) {
txt += z[i].childNodes[0].nodeValue
+ "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
အထက်ဖော်ပြပါ ကုဒ်၏ အထွက်သည်-
A Notebook
Harry Potter
XQuery Kick Start
Learning XML
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
replaceChild() နည်းလမ်းသည် ကလေး node တစ်ခုကို node အသစ်တစ်ခုဖြင့် အစားထိုးသည်။
node အသစ်သည် document ရှိ ရှိပြီးသား node ဖြစ်နိုင်သည်၊ သို့မဟုတ် node အသစ်တစ်ခု ဖန်တီးနိုင်သည်။
အကြံပြုချက်- အစားထိုးထားသော ကလေး node အား တူညီသောစာရွက်စာတမ်းရှိ မည်သည့်ဒြပ်စင်တွင်မဆို နောက်ပိုင်းတွင် ထည့်သွင်းနိုင်ပါသည်။ တူညီသောစာရွက်စာတမ်းထဲသို့ နောက်မှထည့်သွင်း ရန် insertBefore () သို့မဟုတ် appendChild() နည်းလမ်းကို အသုံးပြုပါ၊ သို့မဟုတ် အစားထိုးထားသော node ကို အခြားစာရွက်စာတမ်းထဲသို့ ထည့်သွင်းရန် adoptNode() သို့မဟုတ် importNode() နည်းလမ်းကို အသုံးပြုပါ။
Browser ပံ့ပိုးမှု
replaceChild() နည်းလမ်းကို အဓိက ဘရောက်ဆာအားလုံးတွင် ပံ့ပိုးထားသည်။
အထားအသို
nodeObject.replaceChild(newchild,oldchild)
ကန့်သတ်ချက်များ
Parameter | Type | Description |
---|---|---|
newchild | Node object | Required. The new node to put in the child list |
oldchild | Node object | Required. The node that will be replaced in the child list |
တန်ဖိုးကို ပြန်ပေးသည်။
Type | Description |
---|---|
Node object | The replaced node (oldchild) |
နည်းပညာဆိုင်ရာအသေးစိတ်
DOM ဗားရှင်း | Core Level 1 Node Object DOM အဆင့် 3 တွင် မွမ်းမံထားသည်။ |
---|
❮ Node Object