مетод getAttributeNode() من XML DOM

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

getAttributeNode() يستخرج العنصر من العنصر الحالي بناءً على الاسم.

النحو

elementNode.getAttributeNode(اسم)
المتغيرات وصف
اسم مطلوب. يحدد العنصر الذي يتم استخراج نودته منه.

مثال

السطر التالي سيزيد "books.xml" إلى xmlDoc وسيستخرج "category" الخاص بجميع عناصر <book>:

مفاهيم xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = وظيفة() {
   إذا (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
وظيفة myFunction(xml) {
    مفاهيم x, i, attnode, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    لذا (لذا i = 0; i < x.length; i++) {
        attnode = x.item(i).getAttributeNode("category");
        txt += attnode.name + " = " + attnode.value + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

جرب بنفسك