Properti nodeName, nodeValue, dan nodeType berisi informasi tentang node.
Section Artikel
Di XML DOM, setiap node adalah objek.
Objek memiliki metode dan properti, yang dapat diakses dan dimanipulasi oleh JavaScript.
Tiga properti node penting adalah:
Properti nodeName menentukan nama sebuah node.
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; document.getElementById("demo").innerHTML = xmlDoc.documentElement.nodeName; } </script> </body> </html>
Output :
Properti nodeValue menentukan nilai dari sebuah node.
Kode berikut mengambil nilai simpul teks dari elemen <title> pertama:
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")[0].childNodes[0]; document.getElementById("demo").innerHTML = x.nodeValue; } </script> </body> </html>
Hasil: txt = “Everyday Italian”
Contoh menjelaskan:
Kode berikut mengubah nilai simpul teks dari elemen <title> pertama:
Contoh :
<!DOCTYPE html> <html> <body> <p id="demo1"></p> <p id="demo2"></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; x = xmlDoc.getElementsByTagName("title")[0].childNodes[0]; document.getElementById("demo1").innerHTML = x.nodeValue; x.nodeValue = "Easy Cooking"; x = xmlDoc.getElementsByTagName("title")[0].childNodes[0]; document.getElementById("demo2").innerHTML = x.nodeValue; } </script> </body> </html>
Output :
Penjelasan kode
Properti nodeType menentukan jenis node.
nodeType hanya bisa dibaca.
Jenis node yang paling penting adalah, sebgai berikut:
Node type | NodeType |
---|---|
Element | 1 |
Attribute | 2 |
Text | 3 |
Comment | 8 |
Document | 9 |