Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Your assignment is to take the following html file and implement the code necessary to take the XML file (events.xml), extract the events from the XML document and finally display the extracted information in a table you will create using DOM methods.
Once you’ve created the table, you will need to insert it inside the
(id= “cal”) shown below. Use only the DOM methods that appear in my slides. Do not use innerHTML or similar methods.
HTML
<html>
 <head>
<title>Handling Events</title>
 </head>
<body>
 <div id=""cal""></div>
 <script src="process+events.js"></script>
 <script>
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", processEvents);
oReq.open("GET", "http://pubpages.unh.edu/~jlw/771/events.xml");
oReq.send();
 </script>
 </body>
</html>

You will notice that the each event in the XML file has an empty <map> element. That
element can be ignored when you extract the event information (but don’t remove them
from the XML file, just write code to ignore them).

The JavaScript in the process+event.js file should include the following functions:

A constructor for events – the extracted date from the XML file will be a string, use new Date(datetime) to convert it to a date object. The two other properties will stay strings.

A function called processEvents which extracts the events from the XML file, sorts
them, and finally creates the table used to display them.

A function called collectEvents that extracts the events from the XML file and returns
an array of sorted event objects.

A function called compareDates that is used to sort the events array by datetime. Google for Array.prototype.sort for more information about how to write the
compareDates function.


[Update]
I am attaching the code..I am unable to display the final table. I don't know why?


JavaScript
function event( title,datetime,location)

{
     this.date=new Date(datetime);
     this.title=title;
     this.location=location;
}
function processEvents()
{
     var events;
     if(this.status==200&&this.responseXML!=null)
     {
          events=collectEvents();
     }

     var table=documnet.createElement("TABLE");
     var tableBody=document.createElement("tbody");
     var htmlbody=document.getElementsByTagName('div');
     for(var k1=0;k1<events.length;k1++) {="" var="" tblrow="document.createElement("tr");" col1="document.createElement("td");" col2="document.createElement("td");" col3="document.createElement("td");" col1.appendchild(events[k1].datetime);="" col2.appendchild(events[k1].title);="" col3.appendchild(events[k1].location);="" tblrow.appendchild(col1);="" tblrow.appendchild(col2);="" tblrow.appendchild(col3);="" tablebody.appendchild(tblrow);="" }="" table.appendchild(tablebody);="" htmlbody.appendchild(table);=""  ="" function="" collectevents()="" eventlist="new" array();="" if(this.status="=200&&this.responseXML!=null)" xmlelem="this.responseXML;" title1="xmlElem.getElementsByTagName("title");" datetime1="xmlElem.getElementsByTagName("datetime");" location1="xmlElem.getElementsByTagName("location");" len="title1.length;" for(var="" k1="0;k1<len;k1++)" eventlist.push(new="" event(title1[k1].childnodes[0].nodevalue,datetime1[k1].childnodes[0].nodevalue,location1[k1].childnodes[0].nodevalue));="" neweventlist="eventList.sort(compareDates);" comparedates(date1,date2)="" return="" date1-date2;="" }<="" div="">	<div id="EditDialogPlaceholder"></div>	<div id="ReplyDialogPlaceholder"></div></events.length;k1++)>
Posted
Updated 20-Oct-15 8:34am
v2
Comments
F-ES Sitecore 20-Oct-15 13:29pm    
We're not here to do your homework for you.
Member 12073897 20-Oct-15 14:43pm    
here, i arranged the code properly and attached it again. i even invoked XMLHttp but i dont know why my program doesnt display ouput


function event( title,datetime,location)
{
this.date=new Date(datetime);
this.title=title;
this.location=location;
}
function processEvents()
{
var events;
if(this.status==200&&this.responseXML!=null)
{
events=collectEvents();
}

var table=documnet.createElement("TABLE");
var tableBody=document.createElement("tbody");
var htmlbody=document.getElementsByTagName('div');
for(var k1=0;k1
Patrice T 20-Oct-15 14:54pm    
I updated the question for you.

1 solution

Homework.

Your post says, "Google for Array.prototype.sort", but did you ?

First, not a question at all.
Second, you tried anything so far ?
Third, if you did, where do you stuck ? What's the error ? Any exception ?

Please read this Code Project Quick Answers FAQ[^]

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900