I am looking into blogging technology, specifically using XML files to hold blog posts and reading them into a webpage using Javascript. I have been looking at the w3schools website for bits of code but for some reason my application does not display the content on the screen. My code is as follows:
XML FILE
<?xml version="1.0" encoding="ISO-8859-1"?>
<blogPost>
<title> Test </title>
<text> this is a small tester</post>
<author>William Ahmed</author>
<date>16/03/93</date>
</blogPost>
<blogPost>
<title> Test </title>
<text> this is a small tester</post>
<author>William Ahmed</author>
<date>16/03/93</date>
</blogPost>
<blogPost>
<title> Test </title>
<text> this is a small tester</post>
<author>William Ahmed</author>
<date>16/03/93</date>
</blogPost>
<blogPost>
<title> Test </title>
<text> this is a small tester</post>
<author>William Ahmed</author>
<date>16/03/93</date>
</blogPost>
JAVASCRIPT
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
if(window.XMHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
function load()
{
if(xmlHttp)
{
alert("loadBlog Init");
try
{
xmlHttp.open("GET","blog.xml",true);
xmlHttp.onreadystatechange=handleStateChange();
xmlHttp.send(null);
}
catch(e)
{
alert(e.toString());
}
}
}
function handleStateChange()
{
alert("HandleStateChange");
if(xmlHttp.readystate==4 && xmlHttp.status==200)
{
try
{
handleResponse();
}
catch(e)
{
alert(e.toString());
}
}
else
{
alert(xmlHttp.statusText);
}
}
function handleResponse()
{
alert("HandleResponse");
var xmlResponse = xmlHttp.responseXML;
var root = xmlResponse.documentElement;
var title=root.getElementsByTagName("title");
var text=root.getElementsByTagName("text");
var author=root.getElementsByTagName("author");
var date=root.getElementsByTagName("date");
var blogData="";
for(var i=0; i<title.length; i++)
{
blogData=title.items(i).firstChild.data;
}
document.getElementById("blog").innerHTML=blogData;
}
HTML
<!DOCTYPE html>
<html>
<head>
<script src="readBlog.js"></script>
</head>
<body onload="loadBlog()">
<div id="blog"></div>
</body>
</html>