Quantcast
Viewing latest article 1
Browse Latest Browse All 28

Sorting an XML Document

Sorting an XML based on a date field.

I came across a need to sort an XML based on the date field.I have my code in Java, I could have done the sorting in the Java code via a collection sort easily.But that was not useful in my case. Also I used castor for marshalling the Java ojects into XML.So couldn't find a better way other than to sort the output XML from Castor.
     Below is a skelton of my XML document. In Java code I have a list of Node1 objects and another list of Node2 objects.I need my XML to be sorted in ascending order of the date field, irrespective of Node1 or Node2.(there are other reasons why we have node1 and node 2 seperate)

<root>
    <Node1>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node1>
    <Node1>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node1>
    <Node1>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node1>
    <Node1>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node1>
    <Node2>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node2>
    <Node2>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node2>
    <Node2>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node2>
    <Node2>
        <child1></child1>
        <date>mm/dd/yyyy</date>
<childn></childn>
    </Node2>
</root>

Solution:-
Decided to go with XSLT sorting here, read XSLT will be good in performace as well..Here is how XSL looks like...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates>
              <!-- year -->
<xsl:sort data-type="number" select="substring(date,7,4)"/>
              <!-- day -->
<xsl:sort data-type="number" select="substring(date,1,2)"/>
             <!-- month -->
<xsl:sort data-type="number" select="substring(date,4,2)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Sorting with XSLT was pretty easy.Just use the xsl:sort. But there is no was to sort the dates directly so a little workaround was needed.XSLT used Xpath to travers through the XML and there are many useful built in functions in Xpath.

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

This above part of the XSLT is required to keep the format of the XML.And the Java code to transform goes as below. With couple of lines of code and above xsl file. I got an excellent way of sorting. We can do a lot many pretty cool stuffs with this.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


public class SimpleTransform
{
    public static void main(String[] args)
    throws TransformerException, TransformerConfigurationException,
           FileNotFoundException, IOException
  {
    Transformer transformer = tFactory.newTransformer(new StreamSource("sort.xsl"));
    transformer.transform(new StreamSource("root.xml"), new StreamResult(new FileOutputStream("out.xml")));


    System.out.println("Done.................");
  }
}  

XSLT : http://www.w3schools.com/xsl/


Viewing latest article 1
Browse Latest Browse All 28

Trending Articles