[jQuery] Get Only Certain Parts of XML
Hi,
I have just begun using jQuery to parse an xml file but have hit a
snag and have spent the better part of Sunday trying to figure it out.
What I am trying to do is return only a certain part group of the
below (this is an example but relates to what I am trying to do).
XML file
<students group="a">
<student>
<name>John Lewis</name>
<age>15</age>
<phone>714-555-6677</phone>
<ssn>111-22-3456</ssn>
</student>
<student>
<name post="Class President">Gina Smith</name>
<age>16</age>
<phone>714-777-1234</phone>
<ssn>111-24-1784</ssn>
</student>
<student>
<name>Crystal Morgan</name>
<age>15</age>
<phone>626-566-6134</phone>
<ssn>555-66-7841</ssn>
</student>
</students>
<students group="b">
<student>
<name>Gina Lewis</name>
<age>15</age>
<phone>714-555-6677</phone>
<ssn>111-22-3456</ssn>
</student>
<student>
<name>John Smith</name>
<age>16</age>
<phone>714-777-1234</phone>
<ssn>111-24-1784</ssn>
</student>
<student>
<name post="Vice President">Smith Morgan</name>
<age>15</age>
<phone>626-566-6134</phone>
<ssn>555-66-7841</ssn>
</student>
</students>
JS File
// File: readXML.js
// Start function when DOM has completely loaded
$(document).ready(function(){
// Open the students.xml file
$.get("test.xml",{},function(xml){
// Build an HTML string
myHTMLOutput = '';
myHTMLOutput += '<table width="98%" border="1" cellpadding="0"
cellspacing="0">';
myHTMLOutput += '<th>Name</th><th>Age</th><th>Phone</th><th>SSN</
th>';
// Run the function for each student tag in the XML file
$('student',xml).each(function(i) {
studentName = $(this).find("name").text();
studentAge = $(this).find("age").text();
studentPhone = $(this).find("phone").text();
studentSSN = $(this).find("ssn").text();
studentPost = $(this).find("name").attr("post");
// Build row HTML data and store in string
mydata = BuildStudentHTML
(studentName,studentAge,studentPhone,studentSSN,studentPost);
myHTMLOutput = myHTMLOutput + mydata;
});
myHTMLOutput += '</table>';
// Update the DIV called Content Area with the HTML string
$("#ContentArea").append(myHTMLOutput);
});
});
function BuildStudentHTML
(studentName,studentAge,studentPhone,studentSSN,studentSE){
// Check to see if their is a "post" attribute in the name field
if ((studentPost) != undefined){
studentPostHTML = "<strong>(" + studentPost + ")</strong>";
}
else
{
studentPostHTML = "";
}
// Build HTML string and return
output = '';
output += '<tr>';
output += '<td>'+ studentName + studentPostHTML + '</td>';
output += '<td>'+ studentAge +'</td>';
output += '<td>'+ studentPhone +'</td>';
output += '<td>'+ studentSSN +'</td>';
output += '</tr>';
return output;
}
Output
Name Age Phone SSN
John Lewis 15 714-555-6677 111-22-3456
Gina Smith(Class President) 16 714-777-1234 111-24-1784
Crystal Morgan 15 626-566-6134 555-66-7841
Gina Lewis 15 714-555-6677 111-22-3456
John Smith 16 714-777-1234 111-24-1784
Smith Morgan(Vice President) 15 626-566-6134 555-66-7841
What I would really like to get it just the first three students
(group a) or just the last three students (group b). I am sure there
is an easy way to do this but I have just spent so much time trying
different things and nothing I seem to do works.