tags:

views:

230

answers:

4

Hello I have an XML similiar to below, which needed to be sorted using the date field.

<root> 
    <Node1>
        <date></date> 
    </Node1> 
    <Node1> 
        <date></date> 
    </Node1> 
    <Node1> 
        <date></date> 
    </Node1> 
    <Node1> 
        <date></date> 
    </Node1> 
    <Node2> 
        <date></date> 
    </Node2> 
    <Node2> 
        <date></date> 
    </Node2> 
    <Node2> 
        <date></date> 
    </Node2> 
    <Node2> 
        <date></date> 
    </Node2> 
</root>

I would like to sort the XML based on the date(say asc order), irrespective of whether the date is under Node1 or Node2. Actually in Java code I have two seperate lists, one with Node1 objects and other with Node2 obects. I can sort the list in any order sperately inside java. But I need to have the dates sorted irrespective of the nodes it is apperaing on the XML. What is the best approach to sort this way in Java?

Actaully I am using Castor for marshalling the java objects to XML. If you know this can be done with Castor, that will be great!

+2  A: 

I'd use XSLT, it has probs with sorting dates that you'll need to work round, simplest way if you can control it is to have sortable date format like yyyymmdd

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="root">
    <xsl:copy>
        <xsl:apply-templates>
           <xsl:sort data-type="number" select="date"/>
        </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
      <xsl:copy>
          <xsl:apply-templates/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
blissapp
Thank you............
Java Guy
A: 

If you would like the result of the sort to be a single list, sorted by date then you have to put all of the nodes into a single List of array. If the two types (node1 & node2) extend a common base class, you can use Java's Generics for you list.

List<Node> nodes = new ArrayList<Node>();
nodes.add(node1);
nodes.add(node2);
Node[] nodeArrayToSort = nodes.toArray();

If the two node types do not inherit from a common class, you can simply use a List of Objects.

Now you will have to write your own Comparator. here is an example of one you could use if the node types do have a common super class which holds the Date field.

public class NodeComparator implements Comparator<Node> {
    @Override
    public int compare(Node node1, Node node2) {
        return node1.getDate().compare(node2.getDate());
    }
}

Now that you have your custom comparator and your array with all of your nodes, it is a single line of Java code to sort the list.

Arrays.sort(nodeArrayToSort, new NodeComparator());

The javadoc for the above method can be found here if you would like any additional info on it's behaviour.

Using the above method, it is easy to see how you could write any type of compare function to change the behavior of your sort. You could also write as many custom Comparator classes as you'd please so that you could switch them at runtime. Hope this helps! :)

Gweebz
+1  A: 

hey,

I also think that XSL sorting would be better and fast.

Check the following links,

http://www.codeproject.com/KB/XML/sorting_dates_in_xsl.aspx

http://www.xml.com/pub/a/2002/07/03/transform.html?page=2

http://forums.devx.com/showthread.php?t=4063

thanks.

Paarth
A: 
Java Guy
Cool, it's good to see how to pull it off in Java. Your response looks pretty weird here - scrolling text areas within scrolling text areas - I'm sure that's an SO bug...
blissapp
I didn't do anything intentionally to have two scrolling test areas..Just came up like this.. I am also surprised with this.. :)
Java Guy