XML

Server, tell me your favorite books!

books.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <response> <books> <book> <title> AJAX and PHP: Building Modern Web Applications, 2nd Edition </title> <isbn> 978-1904817726 </isbn> </book> <book> <title> Beginning PHP and MySQL E-Commerce, 2nd Edition </title> <isbn> 978-1590598641 </isbn> </book> <book> <title> Professional Search Engine Optimization with PHP </title> <isbn> 978-0470100929 </isbn> </book> </books> </response>

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www. w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html> <head> <title>AJAX Foundations: JavaScript and XML</title> <script type="text/javascript" src="books.js"></script> </head> <body onload="process()"> <p>Server, tell me your favorite books!</p> <div id="myDivElement" /> </body> </html>

index.js

// contiene il riferimento all'oggetto XMLHttpRequest var xmlHttp = createXmlHttpRequestObject(); // ritrova l'oggetto XMLHttpRequest function createXmlHttpRequestObject() { // contiene il riferimento all'oggetto XMLHttpRequest var xmlHttp; // se il browser è Internet Explorer 6 o più vecchio if(window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { xmlHttp = false; } } // se è Mozilla o altri else { try { xmlHttp = new XMLHttpRequest(); } catch (e) { xmlHttp = false; } } // ritorna l'oggetto o errore if (!xmlHttp) { alert("Error creating the XMLHttpRequest object."); } else { return xmlHttp; } } function process() { // se esiste l'oggetto if (xmlHttp) { // cerca di connetterti al server try { // leggo il file sul server xmlHttp.open("GET", "books.xml", true); xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null); } // messaggio di errore catch (e) { alert("Can't connect to server:\n" + e.toString()); } } } // funzione chiamata al variare di onreadystatechange function handleRequestStateChange() { // quando readyState è 4, la risposta è completata if (xmlHttp.readyState == 4) { // controllo che lo stato della richiesta sia OK if (xmlHttp.status == 200) { try { // fai qualcosa con la risposta del server handleServerResponse(); } catch(e) { // display error message alert("Error reading the response: " + e.toString()); } } else { // display status message alert("There was a problem retrieving the data:\n" + xmlHttp.statusText); } } } // gestisce la risposta del server function handleServerResponse() { // recupera l'XML var xmlResponse = xmlHttp.responseXML; // trova il nodo root xmlRoot = xmlResponse.documentElement; // trova titolo ed ISBN titleArray = xmlRoot.getElementsByTagName("title"); isbnArray = xmlRoot.getElementsByTagName("isbn"); // genera l'HTML di uscita var html = ""; // itera tra gli array per creare la struttura HTML for (var i=0; i<titleArray.length; i++) { html += titleArray.item(i).firstChild.data + ", " + isbnArray.item(i).firstChild.data + "<br/>"; } // trova un riferimento al placeholder HTML sulla pagina myDiv = document.getElementById("myElement"); // inserisci l'HTML myDiv.innerHTML = "<p>Server says: </p>" + html; }