473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.XMLDO M");
function loadXML (xmlFile) {
xmldoc.load(xml file);
friends = xmldoc.document Element;
}

loadXML ('friends.xml') ;
friends = friends.childNo des;
Below is a look at the XML file, friends.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<friends>
<person>
<firstname>Mart in</firstname>
<surname>Bishop </surname>
<age>24</age>
</person>
<person>
<firstname>Robe rt</firstname>
<surname>Kenned y</surname>
<age>18</age>
</person>
<person>
<firstname>Nige l</firstname>
<surname>MacInn es</surname>
<age>23</age>
</person>
</friends>
Before I output the values of each node
(friends.childN odes[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 4819
(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.c om/javascript/sorttable/>

Recent thread discussing table sort:

<URL:http://groups-beta.google.com/group/comp.lang.javas cript/tree/browse_frm/thread/d2e13db951e3db7 7/b3fdc2615655cf3 d?rnum=1&hl=en& q=sort+table&_d one=%2Fgroup%2F comp.lang.javas cript%2Fbrowse_ frm%2Fthread%2F d2e13db951e3db7 7%2F757b0bb0092 a4b7d%3Fq%3Dsor t+table%26rnum% 3D2%26hl%3Den%2 6#doc_b3fdc2615 655cf3d>
--
Rob
Aug 17 '05 #3
RobG wrote:
Reasonable table sort routine on Matt Kruze's site:
<URL:http://www.mattkruse.c om/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.c om/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.o rg/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_getInnerTex t' 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.o rg/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.getEle mentById('tabl' ).getElementsBy TagName('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.o rg/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.getEle mentById('table A');
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.getEle mentById('tabl' ).getElementsBy TagName('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="javascrip t:alert(t.rows[0].cells[0].innerHTML)">te st</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
1503
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 undefined. The show_hide_Div function is meant to be called when a Radio button is selected. See below. Can anyone help function show_hide_Div(theDiv,DivsToHide,TheDivToShow) { elDiv = document.all.theDiv;
5
17856
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 = pnlProperties.all; And then I browse through te collection with for loop doing what I need
4
3310
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 auto sort by the first column without user clicking on it. Here is the sorttable.js code. addEvent(window, "load", sortables_init); var SORT_COLUMN_INDEX;
5
2735
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 the appointment collection to populate the calendar control. The performance getting the XML data is fine, but loading the data into the collection is slow. My question/problem is should I be using the collection, a dataset, or something else to...
3
2189
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 code already to parse std XML files into a Collection? All I've found is code where the XML structure is already known... I need to code to work no matter what the structure... it would "figure out" what nodes are parents/children and such then place...
2
9545
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> <Notes> some notes </Notes> </Report> <Report> blah blah blah </Report>
2
2060
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 class has several public properties like FirstName, LastName, and Gender. What steps would it take to allow the collection to be sorted in multiple ways when bound to a DataGrid? In the past, I used the sort property of a DataView containing a...
1
1489
by: news.microsoft.com | last post by:
I want to XmlNode.ChildNodes reSort by any InnerText en....how? thanks very much!!
0
7192
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 the way. First, deep linking is not something that a completely AJAX web site should be able to do by it's very nature of everything being on one page basically. So how can a person deep link to something that is on one page? This question...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.