Click to See Complete Forum and Search --> : Sorting XML data in Javascript


sack_it
03-23-2005, 09:55 AM
I have read in an xml document using javascript and before I display the data I want to be able to sort it in some way. I know of the sort() method for arrays but can I use it for my xml data?

Here's an example of my xml file
<?xml version="1.0" ?>
<products category="big stuff">
<item id="123">
<name ref="abc3">Unnamed Item1</name>
<des>C_Description not available</des>
</item>
<item id="456">
<name ref="abc1">Unnamed Item2</name>
<des>B_Description not available</des>
</item>
<item id="789">
<name ref="abc2">Unnamed Item3</name>
<des>A_Description not available</des>
</item>
</products>

and here's a section of my code

function initatepage(itemcategory, startPos) {
myxmlfile = 'xmls/'+itemcategory+'.xml';

if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.addEventListener('load', loadHandler, false);
xmlDoc.load(myxmlfile);
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function() {
if(xmlDoc.readyState == 4) loadHandler()
};
xmlDoc.load(myxmlfile);
}
else
{
alert(getIncompatMsg());
return;
}

function loadHandler () {
myxmlDoc = xmlDoc.getElementsByTagName('products');
createStorepage(myxmlDoc, itemcategory, startPos);
} // end loadHandler
} // end initatepage

function createStorepage(myxmlDoc, itemcategory, startPos){

MaxxmlItemLen = myxmlDoc[0].childNodes.length;

//Here's where I want to sort the data before it enters the for loop

for (var i = 0; i < xmlItemLen; i++) {

currentNode = myxmlDoc[0].childNodes[i];

name = currentNode.childNodes[0].firstChild.nodeValue;
ref = currentNode.childNodes[0].getAttribute("ref");
shortdes = currentNode.childNodes[1].firstChild.nodeValue;
// then do something with data
} // end for
} // end createStorepage

any help will be greatfully received.

Carl

Khalid Ali
03-23-2005, 10:18 AM
nope, you will have to create an array of data and then you can use sort() on that..(you could use XSL for sorting if you were using that)

sack_it
03-23-2005, 10:29 AM
Unfortunately I'm not using XSL. Wouldn't copying the xml data into an array before sorting it be really inefficient?

Khalid Ali
03-23-2005, 10:33 AM
shouldn't be too bad unless its a huge data. you will need to put data in an array for some processing on it any ways..

sack_it
03-23-2005, 10:43 AM
you will need to put data in an array for some processing on it any ways..

I'm getting a little confused.

Using the code I've posted I can use the data from the xml file in anyway I like but only in the order as it is originally.
To copy it into an array just to sort it seems strange. Is the variable myxmlDoc in my code not a kind of array anyway?

Khalid Ali
03-23-2005, 11:02 AM
essentially it is, but the object does not implemnt array method sort().
Thats why to use sort you will need to explicitly create an array

gregoftheweb
07-08-2005, 02:31 PM
Well one possibility would be like so.

If you are going to eventually output your data to a table you could include this nifty table sorting js library and do a sort at the very last moment.

http://www.kryogenix.org/code/browser/sorttable/

I'm not sure if this would meet your needs but it would be a way to accomplish your goal.

g