在java中对xml元素进行排序

时间:2013-09-11 05:18:35

标签: 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();
  }

} }

2 个答案:

答案 0 :(得分:1)

将数据添加到集合中,然后使用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?

答案 1 :(得分:1)

将每个名称添加到ArrayList

之类的名称
    ArrayList<String> names = new ArrayList<>(25);

    /*...*/

    String lastName = information.getElementsByTagName("last").item(0).getTextContent();
    String firstName = information.getElementsByTagName("first").item(0).getTextContent();
    names.add(lastName + " " + firstName);

    /*.../

    Collections.sort(names);

您可能会发现某些感兴趣的Collections tutorial ......