package com.cloud.count.utils; import com.cloud.count.model.People; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.text.SimpleDateFormat; import java.util.Date; public class XmlUtil { public static People xmlToPeople(String xmlStr){ Document doc = null; People people = new People(); try { doc = DocumentHelper.parseText(xmlStr); Element rootElt = doc.getRootElement(); Element element1 = rootElt.element("ReportData").element("Report"); people.setDateStr(element1.attributeValue("Date")); Element element2 = element1.element("Object").element("Count"); people.setEndTime(element2.attributeValue("EndTime")); people.setStartTime(element2.attributeValue("StartTime")); people.setEnters(Integer.parseInt(element2.attributeValue("Enters"))); people.setExits(Integer.parseInt(element2.attributeValue("Exits"))); people.setTimestamp(Long.parseLong(element2.attributeValue("UnixStartTime"))); // Element element1 = rootElt.element("Properties"); // Element element2 = rootElt.element("RTReport"); // Element element3 = element2.element("RTObject").element("RTCount"); // people.setDateStr(element2.attributeValue("Date")); // people.setEndTime(""); // people.setStartTime(""); // people.setEnters(Integer.parseInt(element3.attributeValue("TotalEnters"))); // people.setExits(Integer.parseInt(element3.attributeValue("TotalExits"))); // people.setTimestamp(Long.parseLong(element1.elementText("TransmitTime"))); } catch (DocumentException e) { e.printStackTrace(); } return people; } public static People realTimeXmlToPeople(String xmlStr){ Document doc = null; People people = new People(); try { doc = DocumentHelper.parseText(xmlStr); Element rootElt = doc.getRootElement(); Element element1 = rootElt.element("Properties"); Element element2 = rootElt.element("RTReport"); Element element3 = element2.element("RTObject").element("RTCount"); people.setDateStr(element2.attributeValue("Date")); people.setEndTime(""); people.setStartTime(""); people.setEnters(Integer.parseInt(element3.attributeValue("TotalEnters"))); people.setExits(Integer.parseInt(element3.attributeValue("TotalExits"))); people.setTimestamp(Long.parseLong(element1.elementText("TransmitTime"))); } catch (DocumentException e) { e.printStackTrace(); } return people; } public static String toXmlStr(String str){ if(str==null) return ""; int start = str.indexOf("xml"); int end = str.lastIndexOf(">"); if(start<2||end<0)return ""; return str.substring(start-2,end+1); } public static void main(String[] args){ String str ="POST / HTTP/1.1\n" + "Host: 192.168.1.158:2010\n" + "Content-Length: 928\n" + "Connection: Keep-Alive\n" + "\n" + "\n" + "\n" + "\n" + "3\n" + "1546402810\n" + "00:b0:9d:19:92:db\n" + "192.168.1.7\n" + "Cam-18453211\n" + "80\n" + "443\n" + "8\n" + "(GMT 08:00) Beijing, Chongqing, Hong Kong, Urumqi\n" + "0\n" + "2500\n" + "18453211\n" + "0\n" + "4.0.2991.2157\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + ""; String sm=new SimpleDateFormat("HH:mm").format(new Date()); System.out.println(sm); } }