在java中对xml元素进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18733189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sort the xml elements in java
提问by Qll
So I need to sort the data from the xml file by last name in java, and here is the xml file
所以我需要在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>
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();
}
} }
} }
回答by rocketboy
Add the data to a collection and sort the collection afterwards using Collections#sort
将数据添加到集合中,然后使用Collections#sort对集合进行排序
Like this:
像这样:
List<Name> names = new ArrayList<Name>();
Collections.sort(names, new Comparator<name>() {
//comparison logic
});
where Name
can be a class.
哪里Name
可以是一个类。
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?
补充阅读:如何使用比较器?
回答by MadProgrammer
Add each name to something like an ArrayList
将每个名称添加到类似 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 tutorialof some interest as well...
您可能还会发现一些感兴趣的收藏教程......