Frage

Ich habe eine XML ähnlich wie unten, die sortiert werden muss, um das Datumsfeld.

<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>

Ich möchte die XML sortieren basierend auf dem Datum (zB aufsteigende Reihenfolge), und zwar unabhängig davon, ob das Datum unter Node1 oder Node2. Eigentlich in Java-Code habe ich zwei separate Listen, eine mit Node1 Objekte und andere mit Knoten2 Objekte. Ich kann die Liste in beliebiger Reihenfolge separat innerhalb java sortieren. Aber ich brauche die Daten zu haben, sortiert unabhängig von den Knoten auf dem XML erscheint. Was ist der beste Ansatz, um diese Art und Weise in Java zu sortieren?

Eigentlich bin ich mit Castor für die Java-Objekte zu XML-Marshalling. Wenn Sie wissen, kann dies mit Castor getan werden, die groß sein wird!

War es hilfreich?

Lösung

würde ich XSLT verwenden, ist es probs mit Sortierdaten hat, dass Sie an der Arbeit rund benötigen, einfachste Weg, wenn Sie es steuern können, ist sortierbar Datum hat Format wie 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>

Andere Tipps

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! :)

I used XSLT and XALAN.

The XSL is as below.. Date is of the format mm/dd/yyyy

<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="substring(date,7,4)"/> <!-- year sort -->
<xsl:sort data-type="number" select="substring(date,1,2)"/> <!-- day sort -->
<xsl:sort data-type="number" select="substring(date,4,2)"/> <!-- month sort -->
</xsl:apply-templates> 
</xsl:copy> 
</xsl:template> 
<xsl:template match="*"> 
<xsl:copy> 
<xsl:apply-templates/> 
</xsl:copy> 
</xsl:template> 
</xsl:stylesheet>

and the java code is

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;

/**
 *  Use the TraX interface to perform a transformation in the simplest manner possible
 *  (3 statements).
 */
public class SimpleTransform
{
    public static void main(String[] args)
    throws TransformerException, TransformerConfigurationException, 
           FileNotFoundException, IOException
  {  
  // Use the static TransformerFactory.newInstance() method to instantiate 
  // a TransformerFactory. The javax.xml.transform.TransformerFactory 
  // system property setting determines the actual class to instantiate --
  // org.apache.xalan.transformer.TransformerImpl.
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Use the TransformerFactory to instantiate a Transformer that will work with  
    // the stylesheet you specify. This method call also processes the stylesheet
  // into a compiled Templates object.
    Transformer transformer = tFactory.newTransformer(new StreamSource("sort.xsl"));

    // Use the Transformer to apply the associated Templates object to an XML document
    // (foo.xml) and write the output to a file (foo.out).
    transformer.transform(new StreamSource("root.xml"), new StreamResult(new FileOutputStream("out.xml")));

    System.out.println("************* The result is in birds.out *************");
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top