Friday

jquery.fn.extend -- parsing XML into an array of objects

I found it most suitable to be able to parse XML into an array of objects so I can iterate through it for display or sorting. So I call it like this: $().loadXML2array('blah.xml'); and then I can iterate over objectArray any which way I like.
Reason for having it an array of objects: makes sorting faster if you use this to generate a sortable table or datagrid.
loadXML2array: function() {
return this.each(function() {
var data = $.ajax({
type: "GET",
url: "echo.php?xml=datatable1",
async: false,
success: function(xml) {
objectArray = new Array();
$(xml).find('row').each(function(i){
rowObj = new Object();
rowObj.col1 = $(this).children('col_1').text();
rowObj.col2 = $(this).children('col_2').text();
rowObj.col3 = $(this).children('col_3').text();
objectArray.push(rowObj);
});
}
})
});
}

No comments: