Advertisement
piffy

cittadino_XML.html

Aug 13th, 2015
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.50 KB | None | 0 0
  1. <h1>Risultati</h1>
  2. <button onclick="GetData();">Premi qui</button>
  3. <p id="nome"></p>
  4.  
  5. <script>
  6. function getNodeValue(obj,tag)
  7. {
  8.         return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
  9. }
  10.  
  11. function GetData() {
  12.     var xhr;
  13.     if (window.XMLHttpRequest)
  14.         xhr = new XMLHttpRequest();
  15.     else if (window.ActiveXObject)
  16.         xhr = new ActiveXObject("Msxml2.XMLHTTP");
  17.     else
  18.         throw new Error("Ajax non è supportato dal browser");
  19.     xhr.onreadystatechange = function () {
  20.         if (xhr.readyState < 4)
  21.            document.getElementById('div1').innerHTML = "Loading...";
  22.        else if (xhr.readyState === 4) {
  23.            if (xhr.status == 200 && xhr.status < 300) {
  24.          response = xhr.responseXML;
  25.          var cittadino = response.getElementsByTagName('cittadino')[0];
  26.          var nome=getNodeValue(cittadino,'nome');
  27.              var famiglia = response.getElementsByTagName('famiglia');
  28.  
  29.                 document.getElementById('nome').innerHTML="Nome: "+nome;
  30.  
  31.                 for (var i=0;i<famiglia.length;i++) {
  32.                 var node = document.createElement("p");
  33.                 var textnode = document.createTextNode("Famigliare "+(i+1)+": "+famiglia[i].firstChild.nodeValue);
  34.                 node.appendChild(textnode);  
  35.                 document.getElementById("nome").appendChild(node);
  36.                     }
  37.                 }
  38.                
  39.        }
  40.    }
  41.    xhr.open('GET', 'cittadino.xml');
  42.    xhr.send(null);
  43. }
  44.  
  45. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement