Metodo getElementsByTagName() del DOM XML

Definizione e uso

getElementsByTagName() Il metodo restituisce un NodeList di tutti gli elementi con il nome specificato.

Sintassi

getElementsByTagName(name)
Parametro Descrizione
name Stringa, che specifica il nome del tag da cercare. Il valore "*" corrisponde a tutti i tag.

Esempio

Il seguente codice carica "books.xml" nella xmlDoc e visualizza il valore dell'elemento <title> primo:

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 xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("title")[0];
    var y = x.childNodes[0];
    document.getElementById("demo").innerHTML =
    y.nodeValue;
{}

Prova personalmente