1 package cn.zhaokai.sax;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PrintWriter;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.xml.parsers.ParserConfigurationException;
15 import javax.xml.parsers.SAXParser;
16 import javax.xml.parsers.SAXParserFactory;
17
18 import org.xml.sax.Attributes;
19 import org.xml.sax.InputSource;
20 import org.xml.sax.Locator;
21 import org.xml.sax.SAXException;
22 import org.xml.sax.XMLReader;
23 import org.xml.sax.helpers.DefaultHandler;
24
25 public class Demo1 extends HttpServlet {
26
27 public void doPost(HttpServletRequest request, HttpServletResponse response)
28 throws ServletException, IOException {
29
30 try {
31 //解决乱码问题
32 request.setCharacterEncoding("UTF-8");
33 response.setContentType("text/html;charset=UTF-8");
34 //得到输入流
35 InputStream inputStream = request.getInputStream();
36 //A single input source for an XML entity
37 InputSource input = new InputSource(inputStream);
38
39 //5步 sax解析
40 //1创建工厂
41 SAXParserFactory factory = SAXParserFactory.newInstance();
42 //得到解析器
43 SAXParser sp= factory.newSAXParser();
44 //3.得到读取器
45 XMLReader reader = sp.getXMLReader();
46 //4.设置内容处理器,TagValueHandler是单独写的函数
47 TagValueHandler handler= new TagValueHandler();
48 reader.setContentHandler(handler);
49 //5.读取xml文档内容
50 reader.parse(input);
51 //返回识别结果
52 List list = handler.getList();
53
54 //定义输出对象,输出到response
55 PrintWriter out = response.getWriter();
56
57 //输出原始消息
58 String content1 = "<xml><ToUserName><![CDATA[gh_1c586f95bd68]]></ToUserName><FromUserName><![CDATA[ogNtkuMTj4uOx-DCvLfu8mUDmubM]]></FromUserName><CreateTime>1403313142</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[软件孵化实验室]]></Content><MsgId>6027184051137754303</MsgId></xml>";
59 String content2 = "<xml><ToUserName><![CDATA[gh_1c586f95bd68]]></ToUserName><FromUserName><![CDATA[ogNtkuMTj4uOx-DCvLfu8mUDmubM]]></FromUserName><CreateTime>1403313091</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[CLICK]]></Event><EventKey><![CDATA[I_SDUT_SIL]]></EventKey></xml>";
60 out.println("原始消息:");
61 if(list.get(3).equals("text"))
62 {
63 out.println(content1);
64 out.println("解析结果:");
65 out.print("FromUserName(来自):");
66 out.println(list.get(1));
67 out.print("ToUserName(发送至):");
68 out.println(list.get(0));
69 out.print("CreateTime(时间):");
70
71 //时间转换
72 int t = Integer.parseInt((String) list.get(2));
73 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
74 out.println(format.format((long)t*1000L));
75
76 out.print("MsgType(消息类型):");
77 out.println(list.get(3));
78 out.print("Content(文本内容):");
79 out.println(list.get(4));
80 out.print("MsgId(文本消息标识号):");
81 out.println(list.get(5));
82 }
83 else
84 {
85 out.println(content2);
86 out.println("解析结果:");
87 out.print("FromUserName(来自):");
88 out.println(list.get(1));
89 out.print("ToUserName(发送至):");
90 out.println(list.get(0));
91 out.print("CreateTime(时间):");
92
93 //时间转换
94 int t = Integer.parseInt((String) list.get(2));
95 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
96 out.println(format.format((long)t*1000L));
97 out.print("MsgType(消息类型):");
98 out.println(list.get(3));
99 out.print("Event(事件):");
100 out.println(list.get(4));
101 out.print("EventKey(事件值):");
102 out.println(list.get(5));
103
104 }
105
106
107
108 } catch (ParserConfigurationException e) {
109 // TODO Auto-generated catch block
110 e.printStackTrace();
111 } catch (SAXException e) {
112 // TODO Auto-generated catch block
113 e.printStackTrace();
114 }
115 }
116 }
117 //解析xml文档
118 class TagValueHandler extends DefaultHandler{
119
120 List <String> list = new ArrayList();
121
122 private String currentTag; //记住当前解析到的是什么标签
123 @Override
124 public void startElement(String uri, String localName, String qName,
125 Attributes attributes) throws SAXException {
126
127 currentTag = qName;
128 }
129 @Override
130 public void endElement(String uri, String localName, String qName)
131 throws SAXException {
132 currentTag = null;
133 }
134 @Override
135 public void characters(char[] ch, int start, int length)
136 throws SAXException {
137
138 if("ToUserName".equals(currentTag)){
139 list.add(new String(ch,start,length));
140 }
141 if("FromUserName".equals(currentTag)){
142 list.add(new String(ch,start,length));
143 }
144 if("CreateTime".equals(currentTag)){
145 list.add(new String(ch,start,length));
146 }
147 if("MsgType".equals(currentTag)){
148 list.add(new String(ch,start,length));
149 }
150 if("Content".equals(currentTag)){
151 list.add(new String(ch,start,length));
152 }
153 else if("Event".equals(currentTag)){
154 list.add(new String(ch,start,length));
155 }
156 if("MsgId".equals(currentTag)){
157 list.add(new String(ch,start,length));
158 }
159 else if("EventKey".equals(currentTag))
160 {
161 list.add(new String(ch,start,length));
162 }
163 }
164
165 public List getList() {
166 return list;
167 }
168 }