JSFiddle - React, Tailwind, and code Playground
by veer712
JavaScript
var books = function(){
this.category = [];
this.title = "";
this.author = "";
this.year = "";
this.price = "";
this.showProperties = function(){
this.ajaxGetBooksXML();
console.log(this.category);
};
this.ajaxGetBooksXML = function(){
try{
request = new XMLHttpRequest();
} catch (trymicrofot) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherms) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch(failed) {
request = null;
}
}
}
if(request === null) {
alert("Ajax is not supported!!");
} else {
var url = "http://www.w3schools.com/xml/books.xml";
request.open("GET",url,true);
request.onreadystatechange = this.parseBooks;
request.send();
}
};
this.parseBooks = function (){
if(request.readyState == 4 && request.status == 200){
var xmlDoc = request.responseXML;
var booksXML = xmlDoc.getElementsByTagName('book');
var cat = [];
for (var i =0; i<booksXML.length;i++){
var book = booksXML[i].getAttribute('category');
var title = booksXML[i].getElementsByTagName('title')[0].childNodes[0].nodeValue;
var author = booksXML[i].getElementsByTagName('author')[0].childNodes[0].nodeValue;
var year = booksXML[i].getElementsByTagName('year')[0].childNodes[0].nodeValue;
var price = booksXML[i].getElementsByTagName('price')[0].childNodes[0].nodeValue;
cat.push(book);
//console.log(book);
}
console.log(cat);
}
};
};
var obj = new books();
obj.ajaxGetBooksXML();
obj.showProperties();