package cn.zhaokai.sax; import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler; public class Demo1 extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { try {
//解决乱码问题
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//得到输入流
InputStream inputStream = request.getInputStream();
//A single input source for an XML entity
InputSource input = new InputSource(inputStream); //5步 sax解析
//1创建工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//得到解析器
SAXParser sp= factory.newSAXParser();
//3.得到读取器
XMLReader reader = sp.getXMLReader();
//4.设置内容处理器,TagValueHandler是单独写的函数
TagValueHandler handler= new TagValueHandler();
reader.setContentHandler(handler);
//5.读取xml文档内容
reader.parse(input);
//返回识别结果
List list = handler.getList(); //定义输出对象,输出到response
PrintWriter out = response.getWriter(); //输出原始消息
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>";
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>";
out.println("原始消息:");
if(list.get(3).equals("text"))
{
out.println(content1);
out.println("解析结果:");
out.print("FromUserName(来自):");
out.println(list.get(1));
out.print("ToUserName(发送至):");
out.println(list.get(0));
out.print("CreateTime(时间):"); //时间转换
int t = Integer.parseInt((String) list.get(2));
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
out.println(format.format((long)t*1000L)); out.print("MsgType(消息类型):");
out.println(list.get(3));
out.print("Content(文本内容):");
out.println(list.get(4));
out.print("MsgId(文本消息标识号):");
out.println(list.get(5));
}
else
{
out.println(content2);
out.println("解析结果:");
out.print("FromUserName(来自):");
out.println(list.get(1));
out.print("ToUserName(发送至):");
out.println(list.get(0));
out.print("CreateTime(时间):"); //时间转换
int t = Integer.parseInt((String) list.get(2));
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
out.println(format.format((long)t*1000L));
out.print("MsgType(消息类型):");
out.println(list.get(3));
out.print("Event(事件):");
out.println(list.get(4));
out.print("EventKey(事件值):");
out.println(list.get(5)); } } catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//解析xml文档
class TagValueHandler extends DefaultHandler{ List <String> list = new ArrayList(); private String currentTag; //记住当前解析到的是什么标签
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException { currentTag = qName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentTag = null;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException { if("ToUserName".equals(currentTag)){
list.add(new String(ch,start,length));
}
if("FromUserName".equals(currentTag)){
list.add(new String(ch,start,length));
}
if("CreateTime".equals(currentTag)){
list.add(new String(ch,start,length));
}
if("MsgType".equals(currentTag)){
list.add(new String(ch,start,length));
}
if("Content".equals(currentTag)){
list.add(new String(ch,start,length));
}
else if("Event".equals(currentTag)){
list.add(new String(ch,start,length));
}
if("MsgId".equals(currentTag)){
list.add(new String(ch,start,length));
}
else if("EventKey".equals(currentTag))
{
list.add(new String(ch,start,length));
}
} public List getList() {
return list;
}
}