// Using Ajax and XML 3
<html>
<head>
<title>Using Ajax and XML</title>
<script language = "javascript">
function getGuest()
{
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
XMLHttpRequestObject.overrideMimeType("text/xml");
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new
ActiveXObject("Microsoft.XMLHTTP");
}
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open("GET", "guests.xml", true);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
var xmlDocument = XMLHttpRequestObject.responseXML;
displayGuest(xmlDocument);
}
}
XMLHttpRequestObject.send(null);
}
}
function displayGuest(xmldoc)
{
var eventsnode, eventnode, peoplenode;
var firstnamenode, lastnamenode, displaytext;
eventsnode = xmldoc.documentElement;
eventnode = eventsnode.firstChild.nextSibling;
peoplenode = eventnode.lastChild.previousSibling;
personnode = peoplenode.firstChild.nextSibling
.nextSibling.nextSibling.nextSibling.nextSibling;
firstnamenode = personnode.firstChild.nextSibling;
lastnamenode = firstnamenode.nextSibling.nextSibling;
displaytext = "The main guest was: " +
firstnamenode.firstChild.nodeValue + ' '
+ lastnamenode.firstChild.nodeValue;
var target = document.getElementById("targetDiv");
target.innerHTML=displaytext;
}
</script>
</head>
<body>
<h1>Using Ajax and XML</h1>
<form>
<input type = "button" value = "Get the celebrity guest"
onclick = "getGuest()">
</form>
<div id="targetDiv" width =100 height=100>
Who was the celebrity guest?
</div>
</body>
</html>
// guests.xml file
<?xml version="1.0"?>
<events>
<event type="informal">
<event_title>15th award ceremony</event_title>
<event_number>1207</event_number>
<subject>gala event</subject>
<date>7/4/2006</date>
<people>
<person attendance="present">
<first_name>Sam</first_name>
<last_name>Edwards</last_name>
</person>
<person attendance="absent">
<first_name>Sally</first_name>
<last_name>Jackson</last_name>
</person>
<person attendance="present">
<first_name>Cary</first_name>
<last_name>Grant</last_name>
</person>
</people>
</event>
</events>
0 comments:
Post a Comment