Section Artikel
Metode createElement () membuat node elemen baru:
Contoh :
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> 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, i, newElement, txt, xmlDoc; xmlDoc = xml.responseXML; newElement = xmlDoc.createElement("edition"); x = xmlDoc.getElementsByTagName("book")[0] x.appendChild(newElement); // Display all elements xlen = x.childNodes.length; y = x.firstChild; txt = ""; for (i = 0; i < xlen; i++) { if (y.nodeType == 1) { txt += y.nodeName + "<br>"; } y = y.nextSibling; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
Output :
Penjelasan Kode :
CreateAttribute () digunakan untuk membuat node atribut baru:
Contoh :
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> 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, newatt, xmlDoc; xmlDoc = xml.responseXML; newatt = xmlDoc.createAttribute("edition"); newatt.nodeValue = "first"; x = xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt); document.getElementById("demo").innerHTML = "Edition: " + x[0].getAttribute("edition"); } </script> </body> </html>
Output :
Penjelasan Kode :
Jika atribut sudah ada, itu diganti dengan yang baru.
Karena metode setAttribute () membuat atribut baru jika atribut tidak ada, metode ini dapat digunakan untuk membuat atribut baru.
Contoh :
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> 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 x = xmlDoc.getElementsByTagName("title"); x[0].setAttribute("edition", "first"); document.getElementById("demo").innerHTML = "Edition: " + x[0].getAttribute("edition"); } </script> </body> </html>
Output :
Penjelasan Kode :
Metode createTextNode () membuat text node baru:
Contoh:
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> 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 x, y, i, newEle, newText, txt; // add an edition element newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("first"); newEle.appendChild(newText); x = xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newEle); // display all elements xlen = x.childNodes.length; y = x.firstChild; txt = ""; for (i = 0; i < xlen; i++) { if (y.nodeType == 1) { txt += y.nodeName + "<br>"; } y = y.nextSibling; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
Output:
Penjelasan Kode :
Metode createCDATASection () membuat node bagian CDATA baru.
Contoh :
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> 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, newCDATA, xmlDoc; xmlDoc = xml.responseXML; newCDATA = xmlDoc.createCDATASection("Special Offer & Book Sale"); x = xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newCDATA); document.getElementById("demo").innerHTML = x.lastChild.nodeValue; } </script> </body> </html>
Output:
Penjelasan Kode :
Metode createComment () membuat node komentar baru.
Contoh :
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> 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, newComment, xmlDoc; xmlDoc = xml.responseXML; newComment = xmlDoc.createComment("Revised April 2015"); x = xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newComment); document.getElementById("demo").innerHTML = x.lastChild.nodeValue; } </script> </body> </html>
Output :
Penjelasan Kode :