File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting XML nodes?

 
pawan chopra
Ranch Hand
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I sort XML file based on a attribute of node using java ?
 
Paul Clapham
Sheriff
Pie
Posts: 20164
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why not.
 
pawan chopra
Ranch Hand
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, actually I forgot to ask How we can do this ?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
structure.JPG
Directory structure
[Thumbnail for structure.JPG]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 415
jQuery Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic