在 Java 中对 XML 进行排序

发布于 2024-09-02 17:55:27 字数 958 浏览 4 评论 0原文

我有一个类似于下面的 XML,需要使用日期字段进行排序。

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

我想根据日期对 XML 进行排序(例如升序),无论日期是在 Node1 还是 Node2 下。实际上,在 Java 代码中,我有两个单独的列表,一个包含 Node1 对象,另一个包含 Node2 对象。我可以在java中单独按任何顺序对列表进行排序。但我需要对日期进行排序,而不管它出现在 XML 上的节点。 在 Java 中以这种方式排序的最佳方法是什么?

实际上,我正在使用 Castor 将 java 对象封送到 XML。如果您知道可以使用 Castor 来完成此操作,那就太好了!

I have an XML similar to below, which needs 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 ascending order), irrespective of whether the date is under Node1 or Node2. Actually, in Java code I have two separate lists, one with Node1 objects and other with Node2 objects. I can sort the list in any order separately inside java. But I need to have the dates sorted irrespective of the nodes it is appearing on the XML. What is the best approach to sort this way in Java?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄意 2024-09-09 17:55:27

我会使用 XSLT,它在排序日期方面存在问题,您需要解决这个问题,如果您可以控制它,最简单的方法是使用可排序的日期格式,例如 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>

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>
臻嫒无言 2024-09-09 17:55:27

如果您希望排序结果是按日期排序的单个列表,则必须将所有节点放入单个数组列表中。如果两种类型(node1 和 node2)扩展了一个公共基类,则可以使用 Java 的泛型来列出列表。

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

如果这两个节点类型不是从公共类继承的,则可以简单地使用对象列表。

现在您必须编写自己的比较器。如果节点类型确实有一个保存日期字段的公共超类,那么您可以使用以下示例。

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

现在您已经有了自定义比较器和包含所有节点的数组,只需一行 Java 代码即可对列表进行排序。

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

上述方法的 javadoc 可以找到 此处如果您想了解有关其行为的任何其他信息。

使用上述方法,很容易看出如何编写任何类型的比较函数来改变排序的行为。您还可以根据需要编写任意数量的自定义比较器类,以便可以在运行时切换它们。希望这有帮助! :)

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

等数载,海棠开 2024-09-09 17:55:27

我使用了 XSLT 和 XALAN。

XSL如下。日期格式为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>

,java代码为

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 *************");
  }
}

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 *************");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文