473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is it possible to sort a collection of XML childNodes?

Hi,

I am about to create a table, where the values are taken from an XML
file, where each column header you can click and it will sort the table
rows at the client side.

I have got to the following stage:
//first the xml file loading part
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function loadXML (xmlFile) {
xmldoc.load(xmlfile);
friends = xmldoc.documentElement;
}

loadXML ('friends.xml');
friends = friends.childNodes;
Below is a look at the XML file, friends.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<friends>
<person>
<firstname>Martin</firstname>
<surname>Bishop</surname>
<age>24</age>
</person>
<person>
<firstname>Robert</firstname>
<surname>Kennedy</surname>
<age>18</age>
</person>
<person>
<firstname>Nigel</firstname>
<surname>MacInnes</surname>
<age>23</age>
</person>
</friends>
Before I output the values of each node
(friends.childNodes[0].childNodes[0].text etc.), is it possible to
arrange / sort them by either firstname, surname or age?

Cheers

Burnsy

Aug 17 '05 #1
8 4780
(continued)

When I say sort, I mean sort each <person> node by one of the child
nodes (firstname, surname or age).

For example, if I were to sort by age I would get the following order:

Robert
Nigel
Martin

Aug 17 '05 #2
bi******@yahoo.co.uk wrote:
Hi,

I am about to create a table, where the values are taken from an XML
file, where each column header you can click and it will sort the table
rows at the client side.


Typically records are sorted at the server when they are extracted from
the database so that they have some default (or maybe user defined)
order when first presented. Once you've created HTML from them, the
browser script does the sorting thereafter. You may have a cookie that
remembers the current sort column and uses it the next time the site is
visited.

There are a number of JavaScript-based table sort routines available,
some allow sorting of the table at the client when it is first loaded.

There are other techniques using XSL but last time I tried that (some
time ago) it was very browser-specific and slow. Things may have
improved but I think XSL/T has either moved to the server or been
largely ignored.
[...]
Reasonable table sort routine on Matt Kruze's site:

<URL:http://www.mattkruse.com/javascript/sorttable/>

Recent thread discussing table sort:

<URL:http://groups-beta.google.com/group/comp.lang.javascript/tree/browse_frm/thread/d2e13db951e3db77/b3fdc2615655cf3d?rnum=1&hl=en&q=sort+table&_done=% 2Fgroup%2Fcomp.lang.javascript%2Fbrowse_frm%2Fthre ad%2Fd2e13db951e3db77%2F757b0bb0092a4b7d%3Fq%3Dsor t+table%26rnum%3D2%26hl%3Den%26#doc_b3fdc2615655cf 3d>
--
Rob
Aug 17 '05 #3
RobG wrote:
Reasonable table sort routine on Matt Kruze's site:
<URL:http://www.mattkruse.com/javascript/sorttable/>


Although, I don't recommend my solution unless you're trying to do table
sorting and still support Netscape4. It was written way back in the day when
Netscape4 was a dominant browser, and client-side table sorting was a
difficult task. These days, if you don't need to support ancient browsers,
much nicer solutions exist.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 18 '05 #4
Matt Kruse wrote:
RobG wrote:
Reasonable table sort routine on Matt Kruze's site:
<URL:http://www.mattkruse.com/javascript/sorttable/>

Although, I don't recommend my solution unless you're trying to do table
sorting and still support Netscape4. It was written way back in the day when
Netscape4 was a dominant browser, and client-side table sorting was a
difficult task. These days, if you don't need to support ancient browsers,
much nicer solutions exist.


An alternative is 'sorttable', which also suggests a data format:

<URL:http://www.kryogenix.org/code/browser/sorttable/>

It probably doesn't work in Safari because of its use of the row.cells
collection, but it should be OK to replace that with row.childNodes
without any harm.

The 'ts_getInnerText' function uses innerText as on optimisation, it
could also test for and use textContent for browsers that support it.

<URL:http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent>
--
Rob
Aug 18 '05 #5
ASM
RobG wrote:

An alternative is 'sorttable', which also suggests a data format:

<URL:http://www.kryogenix.org/code/browser/sorttable/>

It probably doesn't work in Safari because of its use of the row.cells
collection,
No problemo, with Safari sorter links are disabled

but about Safari and/or row.cells collection
what is it ?

for me :
- the nth row collection from table 'tabl' could be something as :
row_n = document.getElementById('tabl').getElementsByTagNa me('TR')[n];
- and I don't know what is a row.cells collection
with Safari I can extract a TD from my 'row_n'
- how could be a nth col collection ?
could also test for and use textContent for browsers that support it.

<URL:http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent>


--
Stephane Moriaux et son [moins] vieux Mac
Aug 18 '05 #6
ASM wrote:
RobG wrote:

An alternative is 'sorttable', which also suggests a data format:

<URL:http://www.kryogenix.org/code/browser/sorttable/>

It probably doesn't work in Safari because of its use of the row.cells
collection,

No problemo, with Safari sorter links are disabled

but about Safari and/or row.cells collection
what is it ?


Try this test:

<table id="tableA">
<tr>
<td>Here is cell one</td>
<td>Here is cell two</td>
</tr>
</table>
<script type="text/javascript">
var t = document.getElementById('tableA');
alert(
"Rows collection supported? "
+ !!(t.rows)
+ "\nCells collection supported? "
+ !!(t.rows[0].cells)
+ "\nCells length available? "
+ !!(t.rows[0].cells.length)
+ "\nCan I access row 0 cell 0? "
+ !!(t.rows[0].cells[0])
+ "\nCan I access row 0 childNode 0? "
+ !!(t.rows[0].childNodes[0])
);
</script>

For Safari 1.0.3 (Mac OS X 10.2.8) I get false for:

t.rows[0].cells.length

and

t.rows[0].cells[0].

But you can usually substitute the childNodes collection for the cells
collection quite happily without any side-effects as the only valid
children of a TR are TDs. The hard part is that Safari pretends to
support 'cells' but you can't access them.

for me :
- the nth row collection from table 'tabl' could be something as :
row_n = document.getElementById('tabl').getElementsByTagNa me('TR')[n];
- and I don't know what is a row.cells collection
with Safari I can extract a TD from my 'row_n'
- how could be a nth col collection ?


Play with table.rows and table.rows[n].cells (try in Firefox too, it
supports rows and cells collections).

[...]
--
Rob
Aug 18 '05 #7
ASM
RobG wrote:
ASM wrote:

but about Safari and/or row.cells collection
what is it ?

Try this test:


I did and did get : true on each line
For Safari 1.0.3 (Mac OS X 10.2.8) I get false for:

t.rows[0].cells.length
and
t.rows[0].cells[0].
with my Safari 1.2.4 (v125.12) (Mac Os 10.3.7)

I get true for both
The hard part is that Safari pretends to
support 'cells' but you can't access them.
depends wich Safari ? (or Os X version ?)
I get "Here is cell one"
with :
<a href="javascript:alert(t.rows[0].cells[0].innerHTML)">test</a>
Play with table.rows and table.rows[n].cells (try in Firefox too, it
supports rows and cells collections).


I did it, and saw IE5.2 does too, all as iCab3
--
Stephane Moriaux et son [moins] vieux Mac
Aug 18 '05 #8
ASM wrote:
RobG wrote:
ASM wrote:

but about Safari and/or row.cells collection
what is it ?


Try this test:

I did and did get : true on each line
For Safari 1.0.3 (Mac OS X 10.2.8) I get false for:

t.rows[0].cells.length
and
t.rows[0].cells[0].

with my Safari 1.2.4 (v125.12) (Mac Os 10.3.7)

I get true for both


Good, seems Apple have included support in 'Panther' and later. Pitty
they don't allow updating their browser independently of the OS (but
they aren't Robinson Cruso on that one...).

[...]

--
Rob
Aug 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joseph | last post by:
I am attempting to create a function that will hide all SPANS within an ided DIV ="idMain", then only display one span, but this function gives an error when it gets to elChildElement as being...
5
by: Mateo | last post by:
Let's say that I have panel control which is container control, and I need to access every child control inside panel control from JScript. I have this line of JS code: var oCollection =...
4
by: jeffsal | last post by:
I am using sorttable.js to sort a table which works fine which allows a user to sort the table by clicking on the column header. Is there some code I could add to the page (onload or something) to...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
3
by: Aaron | last post by:
I'm a newbie so please bear with me... I'm looking to parse XML into a Collection so that I can pass that collection around to functions to extract data from it easily. I'm hoping there is...
2
by: Greg | last post by:
I'm writing a class in C# .... I have a collection calls Reports made up of Report objects. I'm trying to deserialize an XML file that looks like : <Reports> <Report> <Title>some title</Title>...
2
by: Mark | last post by:
Assume you have a strongly typed collection of a class called Person. The strongly typed collection implements IEnumerable so it can be databound to a server control like a DataGrid. The Person...
1
by: news.microsoft.com | last post by:
I want to XmlNode.ChildNodes reSort by any InnerText en....how? thanks very much!!
0
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.