Advertisement
piffy

JSON_parse.html

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