0

So I need to sort the data from the xml file by last name in java, and here is the xml file

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

and here is what I have so far, able to print the code out, but how do I sort them by last name? please help

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

} }

Devendra
  • 1,864
  • 6
  • 29
  • 49
Qll
  • 1
  • 1
  • 1
  • 3

2 Answers2

1

Add the data to a collection and sort the collection afterwards using Collections#sort

Like this:

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

where Name can be a class.

class Name{
 String firstName, lastName;
}

or you can just do:

    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.

Additional read: How to use a comparator?

Community
  • 1
  • 1
rocketboy
  • 9,573
  • 2
  • 34
  • 36
  • could you please give a example, I'm new to java, thank you so much – Qll Sep 11 '13 at 05:23
  • cannot find symbol error even after I import the arrays and collections with line ArrayList names = new ArrayList<>(25) – Qll Sep 11 '13 at 05:42
1

Add each name to something like an 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);

You might find the Collections tutorial of some interest as well...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • cannot find symbol error even after I import the arrays and collections with line ArrayList names = new ArrayList<>(25); – Qll Sep 11 '13 at 05:41
  • Sorry, if you're using Java 6, it should be ` ArrayList names = new ArrayList(25);`. Don't forget to add `import java.util.ArrayList` to you import statements... – MadProgrammer Sep 11 '13 at 05:44
  • I complied, but the last name is still not sorted, still the same result after System.out.println(name);, the list was not sorted – Qll Sep 11 '13 at 05:49