XML DOM replaceChild() 方法

定义和用法

replaceChild()} మెఘదుర్గం ఒక కుమార నోడ్ ను మరొక కుమార నోడ్ తో పునఃస్థాపించడానికి ఉపయోగించబడుతుంది。

విజయవంతంగా పూర్తి అయినప్పుడు, ఈ ఫంక్షన్ పునఃస్థాపించబడిన నోడ్ ను తిరిగి ఇవ్వబడుతుంది, విఫలమైనప్పుడు తిరిగి ఇవ్వబడుతుంది NULL

సంకేతం

elementNode.replaceChild(new_node,old_node)
పరిమాణాలు వివరణ
new_node అవసరం. నూతన నోడ్ ని నిర్దేశించండి
old_node అవసరం. పునఃస్థాపించవలసిన కుమార నోడ్ ని నిర్దేశించండి

ఉదాహరణ

ఈ కోడ్ "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;
    // ఒక book ఎలమెంట్, title ఎలమెంట్ మరియు టెక్స్ట్ నోడ్ సృష్టించండి
    newNode = xmlDoc.createElement("book");
    newTitle = xmlDoc.createElement("title");
    newText = xmlDoc.createTextNode("హలో వరల్డ్");
    // ఈ టెక్స్ట్ నోడ్ ను title నోడ్ కు జతచేయండి
    newTitle.appendChild(newText);
    // ఈ title నోడ్ ను book నోడ్ కు జతచేయండి
    newNode.appendChild(newTitle);
    y = xmlDoc.getElementsByTagName("book")[0];
    // ఈ కొత్త book నోడ్ ను మొదటి book నోడ్ తో పునఃస్థాపించండి
    x.replaceChild(newNode, y);
    z = xmlDoc.getElementsByTagName("title");
    // అన్ని title ని అవుట్పుట్ చేయండి
    for (i = 0; i < z.length; i++) {
        txt += z[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

స్వయంగా ప్రయత్నించండి