This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
sorry, actually I forgot to ask How we can do this ?
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
1
posted
0
It is not going to be simple to do in place in a DOM because Nodes have to be individually removed and inserted - see the methods in org.w3c.dom.Node
How many nodes are we talking about?
How complex is the XML document?
It might be simpler to sort a collection of Node references and use those contents to write a new XML file.
Bill,
I have following directory struture and I have parsed in XML file. Now size can be quite bigger. Actually I am reading this XML file ant using JSONArray of JSONObjects to display same directory structure in JSP page using some javascript code. But my requirement is the tree structure should be in Chronological order. So basically 2008 should always come before than 2009, so I wanted to sort it based on name attribute of XML node. Also If you can suggest me any otherway like sorting JSONArray object that will be also good. I tried sorting using javascript method array.sort but that doesn't work peoperly for me.
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
1
posted
0
IF I understand you correctly, you dont really want to sort the XML nodes, you want to extract a list of data from the nodes and sort that by some criteria before output in JSON format.
NOW: Just look at the way I formulated the problem.
1. extract data from XML nodes to some intermediate form that can be sorted - how about a custom object?
2. sort the resulting list - how about a custom comparator that knows the order you want?
3. output to JSON - how about a method in the custom class that writes JSON to represent the object?
I think I got the idea, let me try to explain my understanding:
1. extract data from XML nodes to some intermediate form that can be sorted - how about a custom object?
I am reading each XML node into a JSONObject , so you mean to say I create a custom object which may extend JSONObject.
I hope I got you in the correct way.
pseudocode:
Read XML node into custom object
create a list of those objects
use Collections.sort(list,custom comparator) to sort that list
Put that list into JSONArray