XML DOM getElementsByTagNameNS() 方法

التعريف والاستخدام

getElementsByTagNameNS() يستعيد هذا النهج NodeList يحتوي على جميع العناصر التي تحتوي على الاسم والنطاق المحددين.

النحو

getElementsByTagNameNS(ns,اسم)
المعدلات وصف
ns الخطاب، يحدد اسم النطاق الذي يتم البحث عنه. القيمة "*" تطابق جميع العلامات.
اسم الخطاب، يحدد العلامة التي يتم البحث عنها. القيمة "*" تطابق جميع العلامات.

مثال

أدناه، يتم تحميل "books.xml" إلى xmlDoc، ويتم إضافة عقدة عنصر بناء مسمى مسبقًا إلى كل عقدة <book>:

تعريفية xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = وظيفة() {
   إذا (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
وظيفة myFunction(xml) {
    تعريفية x, y, z, i, newel, newtext, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    للفورك (i = 0; i < x.length; i++) {
        newel = xmlDoc.createElementNS("p", "edition");
        newtext = xmlDoc.createTextNode("First");
        newel.appendChild(newtext);
       x[i].appendChild(newel);
    }
    // يعرض جميع title و edition
    y = xmlDoc.getElementsByTagName("title");
    z = xmlDoc.getElementsByTagNameNS("p","edition");
    للفورك (i = 0; i < y.length; i++) {
        txt += y[i].childNodes[0].nodeValue +
        " - " +
        z[i].childNodes[0].nodeValue +
        "edition." +
        "Namespace: " +
        z[i].namespaceURI + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

جربها بنفسك