طريقة XML DOM item()

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

item() يستعيد هذا الطريقة عقدة في موضع معين من قائمة العقد.

النحو

item(index)
الم参数 الوصف
index السند

مثال

مثال 1

هذا الكود يحمل "books.xml" إلى xmlDoc، يمرر على عناصر <book> ويطبع قيمة الخاصية category:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, att, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    للدوران (i = 0; i < x.length; i++) {
        att = x.item(i).attributes.getNamedItem("category");
        txt += att.value + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

جربها بنفسك

مثال 2

مرور على عناصر قائمة النودات:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.documentElement.childNodes;
    للدوران (i = 0; i < x.length; i++) { 
        if (x.item(i).nodeType == 1) {
            txt += x.item(i).nodeName + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt; 
}

جربها بنفسك

مثال 3

تغيير قيمة المشروع:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, attlist, att, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    // تعديل قيمة الخاصية category
    للدوران (i = 0; i < x.length; i++) { 
        attlist = x.item(i).attributes;
        att = attlist.getNamedItem("category");
        att.value = ";BESTSELLER";
    }
    // إخراج جميع العناوين و الإصدارات
    للدوران (i = 0; i < x.length; i++) { 
        txt += x[i].getAttribute("category") + "<br>";
    }
    document.getElementById("demo").innerHTML = txt; 
}

جربها بنفسك