Methodu item() ya XML DOM

Mifano na Matumizi

item() Methodu hupinga mti wa orodha ya miti kwenye kina cha namba zilizochaguliwa.

Mambo ya Kiingiliano

item(index)
Mambo Maelezo
index Mwili

Mfano

Mfano 1

Ina kufikia kwa "books.xml" kwenye xmlDoc, kumwambaa kwenye kitabu, na kutangaza thamani ya maelezo wa kiwango cha 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');
    for (i = 0; i < x.length; i++) {
        att = x.item(i).attributes.getNamedItem("category");
        txt += att.value + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

Mafunzo ya Kufikia Kufanya Kazi

Mfano 2

Mwambaa kwenye orodha ya miti ya kikompyuta:

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;
    for (i = 0; i < x.length; i++) { 
        if (x.item(i).nodeType == 1) {
            txt += x.item(i).nodeName + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt; 
}

Mafunzo ya Kufikia Kufanya Kazi

Mfano 3

Badilisha thamani ya kazi:

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");
    // Kusanya ujuzi wa maelezo wa kiwango cha category
    for (i = 0; i < x.length; i++) { 
        attlist = x.item(i).attributes;
        att = attlist.getNamedItem("category");
        att.value = "BESTSELLER";
    }
    // Kichwa cha jumuiya ya title na edition
    for (i = 0; i < x.length; i++) { 
        txt += x[i].getAttribute("category") + "<br>";
    }
    document.getElementById("demo").innerHTML = txt; 
}

Mafunzo ya Kufikia Kufanya Kazi