HTML and CSS Reference
In-Depth Information
EXAMPLE 18.11 ( CONTINUED )
3 function getXMLContents(httpRequest) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
4
var xml = httpRequest.responseXML;
5
var booklist = xml.getElementsByTagName("book") ;
6
for( j=0; j < booklist.length; j++){
var book=booklist[j];
7
var category=book.getAttribute("category");
for(i=0;i<book.childNodes.length; i++){
8
switch(book.childNodes[i].nodeName){
case "title":
title=book.childNodes[i].firstChild.nodeValue;
case "author":
author=book.childNodes[i].firstChild.nodeValue;
case "price":
price=book.childNodes[i].firstChild.nodeValue;
case "published":
published=book.childNodes[i].firstChild.nodeValue;
}
} //End inner for loop
9
var para=document.createElement("p");
var brtag1=document.createElement("br");
var brtag2=document.createElement("br")
var brtag3=document.createElement("br")
10
var categoryTxt=document.createTextNode("Category:
"+category);
var titleTxt=document.createTextNode("Title: " + title);
var authorTxt=document.createTextNode("Author: "+author);
var priceTxt=document.createTextNode("Price: "+ price);
11
para.appendChild(categoryTxt);
para.appendChild(brtag1);
para.appendChild(titleTxt);
para.appendChild(brtag2);
para.appendChild(authorTxt);
para.appendChild(brtag3);
para.appendChild(priceTxt);
12
document.getElementById('data').appendChild(para);
}
}else { alert('There was a problem with the request.');}
}
}
</script>
</head>
Search WWH ::




Custom Search