解析xml檔案-字串分組演算法-list分頁
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
// 找到節點修改節點內的值.elements(“節點名稱”)
SAXReader reader = new SAXReader();
try{
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
Document document = reader.read(new URL(call));
Element rootElement = document.getRootElement();
List l = rootElement.elements().get(5).elements().get(1).elements().get(0).elements();
for (Element e: l) {
if(“Flag”.equals(e.getName())) {
e.setText(“1”);
}
}
String newXml=document.asXML();
System.out.println(newXml);
SAXReader reader = new SAXReader();
try{
List list1 = new ArrayList<>();
Document document = reader.read(new URL(call));
Element rootElement = document.getRootElement();
List l = rootElement.elements().get(5).elements().get(1).elements().get(1).elements().get(2).elements();
l.forEach(element -> {
List visitsElments = element.elements();
VisitInfo visit = new VisitInfo();
visitsElments.forEach(visitElement -> {
// 工號
if (“DoctId”.equals(visitElement.getName())) {
visit.setDoctorCode(visitElement.getTextTrim());
}
// 姓名
if (“DoctName”.equals(visitElement.getName())) {
visit.setDoctorName(visitElement.getTextTrim());
}
if (“RegisterRemain”.equals(visitElement.getName())) {
List l2=visitElement.elements();
l2.forEach(v -> {
// 地址
if (“LocInfo”.equals(v.getName())) {
visit.setRoomName(v.getTextTrim());
}
// 上午
if (“NoonName”.equals(v.getName())) {
visit.setPeriod(v.getTextTrim());
}
// 型別
if (“SessionType”.equals(v.getName())) {
visit.setDoctorType(v.getTextTrim());
}
});
}
});
list1.add(visit);
});
return list1;
}catch (Exception e){
e.printStackTrace();
}
/3、分組演算法 人物編號
public static List OrderSortByGroup(List list) {
Map<String, List> skuIdMap = new HashMap<>();
for (VisitInfo skuVo : list) {
List tempList = skuIdMap.get(skuVo.getDoctorCode());
/如果取不到資料,那麼直接new一個空的ArrayList*/
if (tempList == null) {
tempList = new ArrayList<>();
tempList.add(skuVo);
skuIdMap.put(skuVo.getDoctorCode(), tempList);
} else {
/某個sku之前已經存放過了,則直接追加資料到原來的List裡
tempList.add(skuVo);
}
}
/3、遍歷map,驗證結果*/
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
for (String skuId : skuIdMap.keySet()) {
list1 = skuIdMap.get(skuId);
VisitInfo visitInfo = new VisitInfo();
visitInfo.setDoctorCode(list1.get(0).getDoctorCode());
visitInfo.setDoctorName(list1.get(0).getDoctorName());
visitInfo.setDeptName(list1.get(0).getDeptName());
visitInfo.setRoomName(list1.get(0).getRoomName());
visitInfo.setVisitDate(list1.get(0).getVisitDate());
visitInfo.setDoctorType(list1.get(0).getDoctorType());
for (VisitInfo visit : list1) {
if (“上午”.equals(visit.getPeriod())) {
visitInfo.setPeriod(visit.getPeriod());
visitInfo.setVisitState(visit.getVisitState());
} else if (“下午”.equals(visit.getPeriod())) {
visitInfo.setAfterData(visit.getPeriod());
visitInfo.setAfterVisitSate(visit.getVisitState());
}
}
list2.add(visitInfo);
}
return list2;
}
/2、分頁演算法*/
private List subList(List list, int i, int pagesize) {
int totalcount = list.size();
int pagecount = 0;
int m = totalcount % pagesize;
List<Integer> subList = new ArrayList<>();
List<Object> listAll = new ArrayList<>();
if (m > 0) {
pagecount = totalcount / pagesize + 1;
} else {
pagecount = totalcount / pagesize;
}
if (m == 0) {
subList = list.subList((i - 1) * pagesize, pagesize * (i));
} else {
if (i == pagecount) {
subList = list.subList((i - 1) * pagesize, totalcount);
} else {
subList = list.subList((i - 1) * pagesize, pagesize * (i));
}
}
listAll.add(list.size());
listAll.add(subList);
return listAll;
}