aspose file tools*
The moose likes XML and Related Technologies and the fly likes Sorting XML nodes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Sorting XML nodes?" Watch "Sorting XML nodes?" New topic
Author

Sorting XML nodes?

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 413

Can I sort XML file based on a attribute of node using java ?


Pawan Chopra
SCJP - DuMmIeS mInD
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

I don't see why not.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 413

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: 12836
    
    5
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
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 413

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.



[Thumbnail for structure.JPG]

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12836
    
    5
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?

Bill
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 413

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

Its great idea thanks alot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting XML nodes?