XML DOM createElementNS() روش

تعریف و کاربرد

createElementNS() این روش یک نود عناصر با نام‌فضا ایجاد می‌کند.

این روش یک عنصر Element برمی‌گرداند.

نحوه‌ی نوشتن

createElementNS(ns,نام)
پارامترها توضیح
ns رشته، تعیین نام فضای نام‌ها.
نام رشته، تعیین نام عناصر.

مثال

کد زیر "books.xml" را به xmlDoc بارگذاری می‌کند و به هر عناصر <book> یک نود عناصر با نام‌فضا اضافه می‌کند:

متغیر var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   اگر (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
دستور کار function myFunction(xml) {
    متغیرهای var 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;
}

تجربة بنفسك