sorting xml nodes alphabatically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

sorting xml nodes alphabatically

Post by susrisha »

is there any way i can sort the xml nodes list based on one of the attributes value?

Code: Select all

 
<node id="1" name="xyz">
<myvar>usa</myvar>
</node>
<node id="2" name="dsd">
<myvar>india</myvar>
</node>
......
 
i want to get the list sorted according to myvar..

i am right now using simplexml for parsing and getting the data.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: sorting xml nodes alphabatically

Post by Benjamin »

There is not a simpleXML method for sorting the data, however you can place them into a multidimensional array and sort that based on one of the values.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: sorting xml nodes alphabatically

Post by susrisha »

The idea looks good .. but is there any other native class (like DOMXML) through which i can straight away sort the document based on the element value? I have written myself the function to sort things out but is there a better and easier way?
I have searched through google and found xsl is one of the solutions but it looks tedious to me. Any information is greately appreciated. :)
Post Reply