Kurs Önerileri:

XML DOM length özelliği

Tanım ve Kullanım length

Özellik, yorum düğümünün metninin uzunluğunu (karakter olarak) döner.

commentNode.length

Örnek

Aşağıdaki kod "books_comment.xml" dosyasını xmlDoc'ya yükler ve ilk <title> ögesinden metin düğüm verisini ve uzunluğunu alır:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_comment.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book")[0].childNodes;
    for (i = 0; i < x.length; i++) {
    // Yalnızca yorum düğümleri işlenir
        if (x[i].nodeType == 8) {
            txt += x[i].length + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt;
}

Kişisel Deneyim

Yukarıdaki örnekte, yalnızca yorum düğümlerini işlemek için döngü ve if test cümleleri kullandık. Yorum düğümünün düğüm türü 8'dir.