使用java比较器排序xml [英] sorting xml using java comparator
本文介绍了使用java比较器排序xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<students>
<student>
<data>
<key>stu-id</key>
<value>56170</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Computer Science Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>56171</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Business Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>50278</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Economics Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>60241</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>English Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>58123</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description> Physics Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>56126</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Chemistry Department </dept-data></value>
</data>
</student>
</students>
需要用java对上面的文件进行排序所以我写了一个比较器类
need to sort the above file with java so I wrote a comparator class
public class Comp implements Comparator<StudentType> {
public class Comp implements Comparator<StudentType> {
public int compare(StudentType o1,StudentType o2)
return (o1.getStudentList().get(1).getValue().compareTo(o2.getStudentList().get(1).getValue()));
}
}
打电话为
Calling as
List<StudentType> students = response.getStudents().getStudentList();
Collections.sort(students, comparator);
新的排序档案看起来像这样
New sorted file looks like this
<students>
<student>
<data>
<key>stu-id</key>
<value>56171</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Business Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>56170</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Computer Science Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>56170</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Computer Science Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>50278</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Economics Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>50278</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description>Economics Department </dept-data></value>
</data>
</student>
<student>
<data>
<key>stu-id</key>
<value>58123</value>
</data>
<data>
<key>dept</key>
<value><?xml version="1.0" encoding="UTF-8"?>Description> Physics Department </dept-data></value>
</data>
</student>
</students>
我不知道为什么竞选班级正在排序和删除一些数据并重复它
谢谢
I don''t know why the camparator class is sorting and deleting some data and duplicating it
Thanks
推荐答案
http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html [ ^ ]
如何存储XML中的值?
某些集合倾向于覆盖已经存在的值。
http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html[^]
How do you store the values from the XML?
some Collections have a tendency to overwrite values that are already in.
这篇关于使用java比较器排序xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文