前端之家收集整理的这篇文章主要介绍了
XML字符串转换成List,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package testJava.ari;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class XML字符串转换成List {
/**
* gefei add 2013-7-16 15:01:36
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String out_param = "<dataout><Count>2</Count><PowerOffArea>江宁区1</PowerOffArea><TypeCode>01</TypeCode><StartTime>20130726 08:30:00</StartTime><StopTime>20130726 12:30:00</StopTime><ScopeLen>270</ScopeLen><Scope>1山凹站</Scope><PowerOffArea>江宁区2</PowerOffArea><TypeCode>01</TypeCode><StartTime>20130726 08:30:00</StartTime><StopTime>20130726 12:30:00</StopTime><ScopeLen>270</ScopeLen><Scope>2山凹站</Scope></dataout>";
List l = convertResXMLToList(out_param);
String cur_date = "20130716 15:41:10";
for(int i=0;i<l.size();++i){
System.out.println("PowerOffArea="+((Map)l.get(i)).get("PowerOffArea"));
System.out.println("TypeCode="+((Map)l.get(i)).get("TypeCode"));
System.out.println("StartTime="+((Map)l.get(i)).get("StartTime"));
System.out.println("StopTime="+((Map)l.get(i)).get("StopTime"));
System.out.println("ScopeLen="+((Map)l.get(i)).get("ScopeLen"));
System.out.println("Scope="+((Map)l.get(i)).get("Scope"));
}
System.out.println("在之间="+isBetStaAndStoTime("20130715 15:41:10","20130717 15:41:10",cur_date));
System.out.println("在之间="+isBetStaAndStoTime("20130716 15:40:10","20130716 15:41:10",cur_date));
System.out.println("不在之间="+isBetStaAndStoTime("20130714 15:40:10","20130715 15:41:11",cur_date));
System.out.println("不在之间="+isBetStaAndStoTime("20130717 15:40:10","20130717 15:41:11",cur_date));
}
/**
* 把xml格式的字符串转换成List<Map> 【停限电告知专用】
* @param xmlParam xml格式的字符串
* @return List<Map>
*/
public static List<Map> convertResXMLToList(String out_param) {
List<Map> xmlList = new ArrayList<Map>();
int all_count = 0;// 地市级停电告知的总条数
if(out_param.indexOf("<Count>") != -1){
all_count = Integer.parseInt(out_param.substring(out_param.indexOf("<Count>")+7,out_param.indexOf("</Count>")).toString().trim());
System.out.println("总条数="+all_count);
}
if(all_count > 0){
for(int i=0;i<all_count;++i){
Map xmlMap = new HashMap();
// 有缺陷 永远是第一次
xmlMap.put("PowerOffArea",out_param.substring(out_param.indexOf("<PowerOffArea>")+14,out_param.indexOf("</PowerOffArea>")).toString());
xmlMap.put("TypeCode",out_param.substring(out_param.indexOf("<TypeCode>")+10,out_param.indexOf("</TypeCode>")).toString());
xmlMap.put("StartTime",out_param.substring(out_param.indexOf("<StartTime>")+11,out_param.indexOf("</StartTime>")).toString());
xmlMap.put("StopTime",out_param.substring(out_param.indexOf("<StopTime>")+10,out_param.indexOf("</StopTime>")).toString());
xmlMap.put("ScopeLen",out_param.substring(out_param.indexOf("<ScopeLen>")+10,out_param.indexOf("</ScopeLen>")).toString());
xmlMap.put("Scope",out_param.substring(out_param.indexOf("<Scope>")+7,out_param.indexOf("</Scope>")).toString());
out_param = out_param.substring(out_param.indexOf("</Scope>")+8,out_param.length());
xmlList.add(xmlMap);
}
}
return xmlList;
}
/**
* 判断当前时间是否在停电开始和结束时间之内
* @param StartTime 停电开始时间
* @param StopTime 停电结束时间
* @return true=在之间 false=不在之间
* @throws Exception
*/
public static boolean isBetStaAndStoTime(String StartTime,String StopTime,String curTime) throws Exception{
boolean isFlag = false;// 默认当前日期不在开始和结束时间之间
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date d1 = sdf.parse(StartTime);// 停电开始时间
Date d2 = sdf.parse(StopTime);// 停电结束时间
Date d3 = sdf.parse(curTime);// 当前时间
long l1 = d1.getTime();// 开始
long l2 = d2.getTime();// 结束
long l3 = d3.getTime();// 当前
if(l3 >= l1 && l3 <= l2){
isFlag = true; // 当前日期在开始和结束时间之间
}
return isFlag;
}
}
原文链接:https://www.f2er.com/xml/299010.html