Sort xml file can this be done?

vbMarkO

Well-known member
Joined
Aug 19, 2005
Messages
157
I have created a xml file of which I am adding records to and thus they are not being added by me the user in a sorted order....

So, my thought is, could I call for a sort routine that would sort it?

If so could you point me to it so I might see how this is done...

My xml is somewhat like this

Code:
<ChurchMemberShip>
       <Members>
            <Name>Smith, John</Name>
            <Address>Here Street</Address>
       </Members>
       <Members>
            <Name>Davis, Bob</Name>
            <Address>There Street</Address>
       </Members>
</ChurchMemberShip>

Could I sort this to look like this

Code:
<ChurchMemberShip>
       <Members>
            <Name>Davis, Bob</Name>
            <Address>There Street</Address>
       </Members>
       <Members>
            <Name>Smith, John</Name>
            <Address>Here Street</Address>
       </Members>
</ChurchMemberShip>

If so how?

vbMarkO
 
To the best of my knowledge there is no way of sorting an xml file. If you wish to sort the data contained in the xml file, the only way I know of achieving it is to load the data into some kind of array sort it in your application. Then write out the array as a new xml file.
 
"Sorting" goes against the whole concept of XML really. The order of the nodes in the file dont matter at all -- its all about the tags and hierarchy of the nodes in relation to one another. Cags is right, youll have to put the data into some kind of an array or list and sort it yourself when you need it.
 
Ok, Makes sense!

I know I can sort it in a control as well such as a Combo or listbox.

Though it would seem, I could also use the Insertafter or insertbefore to make it search for the right place to place the node perhaps so as it writes to the xml it is putting it in a sorted order..... Does this sound reasonable?

Or should I just forgo that and simply add the contents of the xmlfile to an array and sort the array then simply overwrite the existing xml with the sorted array?

vbMarkO
 
"Order" is an arbitrary concept in XML. I wouldnt worry about it to much. That tags are what hold the information. If you really, really needed to have your XML sorted (even though it makes no sense) then writing the nodes out from a sorted list would do the trick. And again, reading them in and sorting them yourself when you need them sorted would also work for whatever application you are making...

Ive never used InsertBefore or InsertAfter -- so knowing nothing about them Id wager that getting that technique to work for you will be more work than it is worth. Youd probably have to navigate the DOM to figure out where you want to make your insert which will be, essentially, a linear search for every insert. Youd be better off using a quick sort algorithm in a List and writing it out in order.

But again...it doesnt really matter because XML order doesnt make sense. Context is taken from the tags, not the order of nodes.
 
XSLT doesnt change node ordering in the document, though.

(Not that it matters because that sounds like a great solution)

vbMarkO, why do you need your XML document sorted?
 
Back
Top