0

I have the following XML data which represents one course, now i have many course elements in my xml and i need to loop horugh them all and display times and course code in correct places in the html table.

<course code="Philo">
            <dayofWeek> Monday
                <hoursofClass>
                    <hour>0800</hour>
                    <hour>0900</hour>
                </hoursofClass>
                <roomNumber>B2029</roomNumber>
            </dayofWeek>

        <dayofWeek> Wednesday
                <hourofClass>
                    <hour>1000</hour>
                    <hour>1100</hour>
                </hourofClass>
                <roomNumber>M3045</roomNumber>
            </dayofWeek>
        </course>

I need to insert into time into correct timetable stots. My HTML table is. Im new to programming and would like some advice. Does My xml need to be reformatted?

<table id="myTable">

<!--DATE HEADING--> 
 <tr>
   <td></td>
    <td>Monday</td>
    <td>Tuesday</td>
     <td>Wednesday</td>
    <td>Thursday</td>
     <td>Friday</td>
  </tr>

  <tr>
   <td>800</td>
    <td id = "m800"></td>
    <td id ="t800"></td>
     <td id = "w800"></td>
    <td id="th800"></td>
     <td id ="f800"></td>
  </tr>

    <tr>
   <td>900</td>
   <td id = "m900"></td>
    <td id ="t900"></td>
     <td id = "w900"></td>
    <td id="th900"></td>
     <td id ="f900"></td>
  </tr>

    <tr>
   <td>1000</td>
    <td id = "m1000"></td>
    <td id ="t1000"></td>
     <td id = "w1000"></td>
    <td id="th1000"></td>
     <td id ="f1000"></td>
  </tr>
Derek
  • 47
  • 1
  • 9

2 Answers2

0

As we want you to learn by yourself how to code this, I'll (hopefully) give you all the relevant parts you need to get you started.

Steps:

  1. Modify your XML so that you can find the day prefix more easily; otherwise you will have to use switch case in your JavaScript code to translate the day name into the prefix.
  2. Load the XML data on your page containing the class table. To make life easier, I would recommend to not use XMLHttpRequest but use a library like JQuery, loading your data is then as easy as:
$.ajax('PATHTOXML.xml', {
    success: function(data) {
        // your XML code will be available inside the "data" variable
    }   
});
  1. Loop over your loaded data in a loop. The following functions may be handy when you access your XML object:

    • data.children[0].getAttributeNode("code").value will return "Philo".
    • data.children[0].hasAttribute("code") will return TRUE for the same entry.
    • data.children.length can be used to check for the number of elements (or the length of a contained value in case of strings or numbers), it will return 1 for the base element.
    • data.getElementsByTagName("course")[0].getElementsByTagName("dayofWeek") will access the first element (JS uses 0 as base for counting) with the XML tag name "course" and will then return the values for the tag name "dayofWeek"

    • More examples can be found in this SO Q&A and in this blog post

  2. add the data to your table. I will use a static code example (without any error handling, should be different in your code) to show how this could be done:

var courseName= data.children[0].getAttributeNode("code").value;
var dayName= data.getElementsByTagName("course")[0].getElementsByTagName("dayofWeek")[0].childNodes[0].textContent;
var start= data.getElementsByTagName("course")[0].getElementsByTagName("dayofWeek")[0].getElementsByTagName("hoursofClass")[0].getElementsByTagName("hour")[0].textContent;
var end= data.getElementsByTagName("course")[0].getElementsByTagName("dayofWeek")[0].getElementsByTagName("hoursofClass")[0].getElementsByTagName("hour")[1].textContent;
var room= data.getElementsByTagName("course")[0].getElementsByTagName("dayofWeek")[0].getElementsByTagName("roomNumber")[0].textContent;

document.getElementById(dayName+start).innerHTML += courseName + ' in ' + room + '<br />' + start + ' - ' + end + '<br />';

If you want to loop over a structure, you can use code similar to the following:

// accesses the first element matching the tag "course"
var courseData= data.getElementsByTagName("course")[0];
// retrieves the node attribute value of attribute "code"
var courseName= courseData.getAttributeNode("code").value;
var courseDaysData= courseData.getElementsByTagName("dayofWeek");
// loop over all course days below one course
for (var icd=0; icd<courseDaysData.length; icd++) {
  // loop over your days
}

General hints:

  • When running your page in your browser of your choice, open the "Developer Tools" (shortcut should be F12) that will show errors in your code, allow you to position breakpoints to stop your code exactly on the relevant points and much more. It's the most handy tool for JS code debugging that you'll find.
  • To see the values of variables, you can add debug output statements in your code. To do so, use console.log(VARIABLE);, i.e. console.log(dayName);. You will find the output in said Developer Tools.

Regarding your XML:

As mentioned before, the day name (or prefix) in your HTML and XML should match, otherwise you need a "switch case". Besides that, your current XML code just allows you to have different times for one class in one room, I don't know if that is what you wanted to have.

Let me know if you need any more explanation. Good learning!

SaschaM78
  • 4,376
  • 4
  • 33
  • 42
  • document.getElementById(dayName+start).innerHTML is giving me an error, however when I type in document.getElementById(Monday0800).innerHTML i get the right output any pointers? – Derek Feb 18 '18 at 18:01
  • how would the code work inside of a for loop of all courses? – Derek Feb 18 '18 at 19:47
  • I assume your `dayName` and `start` variables have not been initialised. You should use the Developer Tools in your browser (press F12 when running your page) and use `console.log()` to debug your code - I'll add this to my code. I'll also add a sample for a loop. – SaschaM78 Feb 19 '18 at 08:47
  • Check the updated code above, especially the "Hints" section. – SaschaM78 Feb 19 '18 at 13:05
  • i am trying to traverse now the second set of hours in the first course however it doesnt seem to be working; how come var start= courseData[0].getElementsByTagName("dayofWeek")[1].getElementsByTagName("hoursofClass")[0].getElementsByTagName("hour")[0].textContent; doesnt return 1000? – Derek Feb 20 '18 at 08:19
  • You have an error in your XML code, the second `dayofWeek` node contains `hourofClass`, not `hoursofClass`, you missed the "s". Could you post the code that you already created to your question? That makes it much easier to find problems in your code. – SaschaM78 Feb 20 '18 at 10:23
  • ahh howd i miss that. thank you i know have figured it out – Derek Feb 21 '18 at 00:08
0

Hope This Answers your Question Just Copy & Paste into an HTML file for testing.

var XML = ''
+'<course id="Philo">'
+'            <dayofWeek>Monday'
+'                <hoursofClass>'
+'                    <hour>8</hour>'
+'                    <hour>9</hour>'
+'                </hoursofClass>'
+'                <roomNumber>B2029</roomNumber>'
+'            </dayofWeek>'
+''
+'        <dayofWeek>Wednesday'
+'                <hoursofClass>'
+'                    <hour>10</hour>'
+'                    <hour>11</hour>'
+'                </hoursofClass>'
+'                <roomNumber>M3045</roomNumber>'
+'            </dayofWeek>'
+'</course>';
    XML = (new DOMParser()).parseFromString(XML, "text/xml");
console.log(XML);

console.log(XML.getElementsByTagName("course"));
XML.getElementsByTagName("course");
  for(N_01=0; N_01<XML.getElementsByTagName("course").length; N_01++){ 
  console.log(XML.getElementsByTagName("course")[N_01]); 
   DAY_OF_WEEK = XML.getElementsByTagName("course")[N_01].getElementsByTagName('dayofWeek');
      ///
      ///
      ///
      for(N_02=0; N_02<DAY_OF_WEEK.length; N_02++){ 
          CLASS_HOUR = DAY_OF_WEEK[N_02].getElementsByTagName('hoursofClass');
          CLASS      = DAY_OF_WEEK[N_02].getElementsByTagName('roomNumber');
              ///
          ///
          //
          for(N_03=0; N_03<CLASS_HOUR.length; N_03++){
              ///
              ///
              ///
              HOURS = CLASS_HOUR[N_03].getElementsByTagName('hour');
              for(N_04=0; N_04<HOURS.length; N_04++){
              var ROW = document.getElementById('TIME_'+HOURS[N_04].innerHTML);
              var CELL= '';
                  if(DAY_OF_WEEK[N_02].innerHTML.search('Monday')!=-1){   CELL=1;  }
                  if(DAY_OF_WEEK[N_02].innerHTML.search('Tuesday')!=-1){  CELL=2;  }
                  if(DAY_OF_WEEK[N_02].innerHTML.search('Wednesday')!=-1){CELL=3;  }
                  if(DAY_OF_WEEK[N_02].innerHTML.search('Thursday')!=-1){ CELL=4;  }
                  if(DAY_OF_WEEK[N_02].innerHTML.search('Friday')!=-1){   CELL=5;  }
              ROW.cells[CELL].innerHTML = CLASS[N_03].innerHTML;
              }//
              ///
              ///

          }//
          ///
          ///
      }//
      ///
      ///
  }
#myTable TD{text-align:center; padding:5px;}
#myTable .td_1{text-align:right; padding:5px; padding-left:15px;}
<table id="myTable" cellspacing=1 cellpadding=1 border=1>
<!--DATE HEADING--> 
<tr><td></td>
    <td>Monday</td>
    <td>Tuesday</td>
    <td>Wednesday</td>
    <td>Thursday</td>
    <td>Friday</td>
<tr ID=TIME_8><td class=td_1>8</td>
    <td id = "m800"></td>
    <td id ="t800"></td>
    <td id = "w800"></td>
    <td id="th800"></td>
    <td id ="f800"></td>
<tr ID=TIME_9><td class=td_1>9</td>
    <td id = "m800"></td>
    <td id ="t800"></td>
    <td id = "w800"></td>
    <td id="th800"></td>
    <td id ="f800"></td>
<tr ID=TIME_10><td class=td_1>10</td>
    <td id = "m800"></td>
    <td id ="t800"></td>
    <td id = "w800"></td>
    <td id="th800"></td>
    <td id ="f800"></td>
<tr ID=TIME_11><td class=td_1>11</td>
    <td id = "m800"></td>
    <td id ="t800"></td>
    <td id = "w800"></td>
    <td id="th800"></td>
    <td id ="f800"></td>
</TABLE>
Matthew
  • 49
  • 5