1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Techniques AJAX - XMLHttpRequest</title>
<script type="text/javascript">
<!--
function getXMLHttpRequest() {
var xhr = null;
if (window.XMLHttpRequest || window.ActiveXObject) {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhr = new XMLHttpRequest();
}
} else {
alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
return null;
}
return xhr;
}
function request(callback) {
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
lire(xhr);
//callback(xhr.responseXML);
//alert(xhr.responseText);
}
};
xhr.open("GET", "fichierXML.xml", true);
xhr.send(null);
}
function lire(xhr)
{
t = "<table border='1px'>";
xmlDoc=xhr.responseXML;
x=xmlDoc.getElementsByTagName("Tag"); <!-- On se base sur le nombre de balises TAGs -->
for (i=0;i<x.length;i++)
{
y = z = "";
t +="<tr>";
y=xmlDoc.getElementsByTagName("Tag")[i].getAttribute("raw"); <!-- On récupère l'attribut RAW -->
t +="<td>"+y+"</td>";
z=xmlDoc.getElementsByTagName("Tag")[i].getAttribute("rssi"); <!-- On récupère l'attribut RSSI -->
t +="<td>"+z+"</td>";
t +="</tr>";
}
t += "</table>";
document.getElementById("output").innerHTML=t
}
//-->
</script>
</head>
<body>
<p>
<button onclick="request();">Afficher le fichier XML</button>
<div id="output"></div>
</p>
</body>
</html> |
Partager