Parsing XML via jQuery, nested loops
- by Coughlin
I am using jQuery to parse XML on my page using $.ajax(). My code block is below and I can get this working to display say each result on the XML file, but I am having trouble because each section can have MORE THAN ONE and im trying to print ALL grades that belong to ONE STUDENT. Here is an example of the XML.
<student num="505">
<name gender="male">Al Einstein</name>
<course cid="1">60</course>
<course cid="2">60</course>
<course cid="3">40</course>
<course cid="4">55</course>
<comments>Lucky if he makes it to lab, hopeless.</comments>
</student>
Where you see the I am trying to get the results to print the grades for EACH student in each course. Any ideas on what I would do?
Thanks,
Ryan
$.ajax({
type: "GET",
url: "final_exam.xml",
dataType: "xml",
success: function(xml) {
var student_list = $('#student-list');
$(xml).find('student').each(function(){
$(xml).find('course').each(function(){
gradeArray = $(this).text();
console.log(gradeArray);
});
var name = $(this).find("name").text();
var grade = $(this).find("course").text();
var cid = $(this).find("course").attr("cid");
//console.log(cid);
student_list.append("<tr><td>"+name+"</td><td>"+cid+"</td><td>"+grade+"</td></tr>");
});
}
});