mynameisdave145
12-06-2006, 09:36 PM
ok, Ive got a script I am using that reads data from an XML file, then outputs that information into a table when a link is clicked. It works great as it is right now, however I want to be able to sort the information in different ways and am not sure what the best way to do this would be...
Script that works to simply display information into a table:
function getData()
{
output = '<table width="450" border="1" cellspacing="1" cellpadding="1"><tr><td width="100"><b>Word</b></td><td width="50"><b>Section</b></td><td width="200"><b>Definition</b></td>';
var wrds = xmlDoc.getElementsByTagName("definition");
for (var i=0; i<wrds.length; i++)
{
var word = xmlDoc.getElementsByTagName("word")[i].childNodes[0].nodeValue;
var section = xmlDoc.getElementsByTagName("section")[i].childNodes[0].nodeValue;
var def = xmlDoc.getElementsByTagName("def")[i].childNodes[0].nodeValue;
output += '<tr><td>' + word + '</td><td>' + section + '</td><td>' + def + '</td></tr>';
}
document.getElementById('glossaryDiv').innerHTML = output;
}
XML file format:
<glossary>
<definition id="1">
<word>Music</word>
<section>Main</section>
<def>Music is great</def>
</definition>
<definition id="2">
<word>Abstract</word>
<section>Main</section>
<def>Of or pertaining to the formal aspect of art, emphasizing lines, colors, generalized or geometrical forms, etc., esp. with reference to their relationship to one another; pertaining to the nonrepresentational art styles of the 20th century.</def>
</definition>
...etc...
</glossary>
I just want to write a new function that will say sort this alphabetically, or by section or whatever else... any help would be greatly appreciated...
Script that works to simply display information into a table:
function getData()
{
output = '<table width="450" border="1" cellspacing="1" cellpadding="1"><tr><td width="100"><b>Word</b></td><td width="50"><b>Section</b></td><td width="200"><b>Definition</b></td>';
var wrds = xmlDoc.getElementsByTagName("definition");
for (var i=0; i<wrds.length; i++)
{
var word = xmlDoc.getElementsByTagName("word")[i].childNodes[0].nodeValue;
var section = xmlDoc.getElementsByTagName("section")[i].childNodes[0].nodeValue;
var def = xmlDoc.getElementsByTagName("def")[i].childNodes[0].nodeValue;
output += '<tr><td>' + word + '</td><td>' + section + '</td><td>' + def + '</td></tr>';
}
document.getElementById('glossaryDiv').innerHTML = output;
}
XML file format:
<glossary>
<definition id="1">
<word>Music</word>
<section>Main</section>
<def>Music is great</def>
</definition>
<definition id="2">
<word>Abstract</word>
<section>Main</section>
<def>Of or pertaining to the formal aspect of art, emphasizing lines, colors, generalized or geometrical forms, etc., esp. with reference to their relationship to one another; pertaining to the nonrepresentational art styles of the 20th century.</def>
</definition>
...etc...
</glossary>
I just want to write a new function that will say sort this alphabetically, or by section or whatever else... any help would be greatly appreciated...