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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>restituer un xml</title>
<script type='text/JavaScript'>
function getXhr(){
var xhr = null;
if(window.XMLHttpRequest) // Firefox et autres
xhr = new XMLHttpRequest();
else if(window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
xhr = false;
}
return xhr
}
// Node cleaner
function go(c){
if(!c.data.replace(/\s/g,''))
c.parentNode.removeChild(c);
}
function clean(d){
var bal=d.getElementsByTagName('*');
for(i=0;i<bal.length;i++){
a=bal[i].previousSibling;
if(a && a.nodeType==3)
go(a);
b=bal[i].nextSibling;
if(b && b.nodeType==3)
go(b);
}
return d;
}
/**
* Méthode qui sera appelée sur le click du bouton
*/
function exemple(){
document.getElementById("content").innerHTML = "";
var xhr = getXhr();
//var xhr=getXMLRequest();
//xhr.onreadystatechange = handleHttpResponse(xhr);
xhr.onreadystatechange = function () {handleHttpResponse(xhr)};
var url = "dvd.xml";
xhr.open("GET", url, true);
xhr.send(null);
}
function handleHttpResponse(xhr) {
if(xhr.readyState == 4 )
if (xhr.status == 200 || xhr.status == 0) {
var response = cleanXML(xhr.responseXML.documentElement);
var items = response.getElementsByTagName("item");
var html = '';
count = items.length;
for(i = 0; i < count; i++) {
html += '<div class="item">';
html += '<h1>' + items[i].getElementsByTagName("title")[0].firstChild.nodeValue + '</h1>';
html += '<div class="author">Réalisateur : ';
html += items[i].getElementsByTagName("director")[0].firstChild.nodeValue + '</div>';
html += '<div class="price">Prix : ';
html += items[i].getElementsByTagName("price")[0].firstChild.nodeValue + ' ?</div>';
html += '</div>';
}
document.getElementById('content').innerHTML + = html;
}else{
alert("Error:returned status code"+xhr.status +""+xhr.statusText);
}
}
</script>
</head>
<body>
<p>
<a href="javascript:exemple();">cliquer !</a>
<div id="content"></div>
</p>
</body>
</html> |