XML DOM createTextNode() روش

تعریف و استفاده

createTextNode() این روش یک گره متن ایجاد می‌کند.

این روش یک شیء Text را برمی‌گرداند.

قوانین دستور زبان

createTextNode(text)
پارامتر توضیح
text زیر نویسی، تعریف متن گره.

مثال

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

مقادیر xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   اگر (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
دستور کار myFunction(xml) {
    مقادیر x, y, z, i, xLen, yLen, newEle, newText, xmlDoc و txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    xLen = x.length;
    // ایجاد گره‌های عناصر و گره‌های متن
    برای (i = 0; i < xLen; i++) {
        newEle = xmlDoc.createElement("edition");
        newText = xmlDoc.createTextNode("first");
        newEle.appendChild(newText);
        x[i].appendChild(newEle);
    }
    // همه title و edition را چاپ کن
    y = xmlDoc.getElementsByTagName("title");
    yLen = y.length
    z = xmlDoc.getElementsByTagName("edition");
    برای (i = 0; i < yLen; i++) {
        txt += y[i].childNodes[0].nodeValue +
        " - Edition: " +
       z[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

آزمایش کنید