XML DOM createElement() روش
تعریف و استفاده
createElement()
روش ایجاد نود نود.
این روش Element نود را برمیگرداند.
زبان
createElement(name)
پارامتر | توضیح |
---|---|
name | عبارت، علامت نام نود را مشخص میکند. |
مثال
این کد "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(); دستور العمل myFunction(xml) { مفاهیم var 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; }