java - java中对xml元素进行排序

标签 java xml sorting dom

所以我需要在java中按姓氏对xml文件中的数据进行排序,这是xml文件

        <employeeList>
        <employee>
            <name>
                <last>Johnson</last>
                <first>Jason</first>
            </name>
        </employee>
        <employee>
            <name>
                <last>McGrady</last>
                <first>Mike</first>
            </name>
        </employee>
        <employee>
            <name>
                <last>Allen</last>
                <first>Chris</first>
            </name>
        </employee>
        <employee>
            <name>
                <last>Zeller</last>
                <first>Tom</first>
            </name>
        </employee>

        <employee>
            <name>
                <last>Camp</last>
                <first>Alex</first>
            </name>
        </employee>

这是我到目前为止所拥有的,能够打印出代码,但是如何按姓氏对它们进行排序?请帮忙

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class SortLastName {
   public static void main(String[] args)
 {    
  try{ 
     File employeesList = new File("employees.xml");
     DocumentBuilderFactory employeesFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder employeesBuilder = employeesFactory.newDocumentBuilder();
    Document employees = employeesBuilder.parse(employeesList);
     employees.getDocumentElement().normalize();
     NodeList nEmployeesList = employees.getElementsByTagName("employee");
     int totalEmployees = nEmployeesList.getLength();

  for (int a = 0; a < totalEmployees; a++)
  {
     Node list = nEmployeesList.item(a);   
     if (list.getNodeType() == Node.ELEMENT_NODE)
     {
        Element information = (Element) list;
        String lastName = information.getElementsByTagName("last").item(0).getTextContent();
        String firstName = information.getElementsByTagName("first").item(0).getTextContent();
        System.out.println("Last name: " + lastName );
        System.out.println("First name: " + firstName);
        System.out.println();
        }
     }
     }
  catch(Exception e)
  {
     e.printStackTrace();
  }

} }

最佳答案

将数据添加到集合中,然后使用 Collections#sort 对集合进行排序

像这样:

    List<Name> names = new ArrayList<Name>();
    Collections.sort(names, new Comparator<name>() {
        //comparison logic
    });

其中Name可以是一个类。

class Name{
 String firstName, lastName;
}

或者你可以这样做:

    List<String> names = new ArrayList<String>();
    names.add(firstName + " " + LastName);
    //when list is complete
    Collections.sort(names);// this will use natural(lexical) sort order
                            //, or you can add you own comparator like above.

附加阅读:How to use a comparator?

关于java - java中对xml元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733189/

相关文章:

java - 如何在 Android 应用程序上读取大型 Base64 文件(150MB)?

java - 从地理位置 api 获取响应代码为 400

java - 使用 JAVA 使用 XPATH 将 xml 值存储为 Map

python - 根据对角线项对矩阵进行排序

sorting - Redis:将多个 HSET 排序集分组并汇总为一个排序集

c - 在 C 中不使用指针对结构体数组进行排序

java - 使用 Apache Maths (Java) 的多项式回归强制在 (0,0) 处截距

java - OSB 代理从 AWS S3 检索内容 - java.lang.NoClassDefFoundError :com/amazonaws/services/s3/model/S3ObjectInputStream

javascript - 使用 Javascript 将 xml 加载到类

java - 用Java有效解析Excel数据