微信消息处理JAXP-dom解析
1 package cn.lihainan.test; 2 import java.io.BufferedReader; 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.PrintWriter; 10 import java.text.SimpleDateFormat; 11 12 import javax.servlet.ServletException; 13 import javax.servlet.ServletRequest; 14 import javax.servlet.http.HttpServlet; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 import javax.xml.parsers.DocumentBuilder; 18 import javax.xml.parsers.DocumentBuilderFactory; 19 import javax.xml.parsers.ParserConfigurationException; 20 21 22 import org.dom4j.io.SAXReader; 23 import org.dom4j.io.XMLWriter; 24 25 import org.w3c.dom.Document; 26 import org.w3c.dom.Node; 27 28 29 public class Demo1 extends HttpServlet { 30 public void doPost(HttpServletRequest request, HttpServletResponse response) 31 throws ServletException, IOException { 32 try { // 将请求、响应的编码均设置为UTF-8(防止中文乱码) 33 request.setCharacterEncoding("utf-8"); 34 response.setContentType("textml;charset=utf-8"); 35 InputStream inputStream = request.getInputStream(); 36 37 38 39 String result = readFromInputStream(inputStream); 40 41 42 43 PrintWriter out = response.getWriter(); 44 45 out.println("原始消息:"); 46 47 out.println(result); 48 49 //解析 建立document解析 50 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 51 DocumentBuilder builder; 52 builder = factory.newDocumentBuilder(); 53 54 InputStream xmlStream = new ByteArrayInputStream(result.getBytes("UTF-8")); 55 Document document = builder.parse(xmlStream); 56 //定义节点 根据节点从document中找到要输出的 57 Node ToUserName = document.getElementsByTagName("ToUserName").item(0); 58 System.out.println(ToUserName.getTextContent()); 59 60 Node FromUserName = document.getElementsByTagName("FromUserName").item(0); 61 System.out.println(FromUserName.getTextContent()); 62 63 Node MsgType= document.getElementsByTagName("MsgType").item(0); 64 System.out.println(MsgType.getTextContent()); 65 66 Node Content = document.getElementsByTagName("Content").item(0); 67 System.out.println(Content.getTextContent()); 68 69 Node MsgId= document.getElementsByTagName("MsgId").item(0); 70 System.out.println(MsgId.getTextContent()); 71 72 73 out.println(); 74 //输出 75 out.println("解析结果:"); 76 out.print("FromUserName(来自):"); 77 out.println(FromUserName.getTextContent()); 78 out.print("ToUserName(发送至):"); 79 out.println(ToUserName.getTextContent()); 80 out.print("CreateTime(时间):"); 81 82 Node createTime= document.getElementsByTagName("CreateTime").item(0); 83 int t = Integer.parseInt(createTime.getTextContent()); 84 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); 85 out.println(format.format((long)t*1000L)); 86 out.print("MsgType(消息类型):"); 87 out.println(MsgType.getTextContent()); 88 out.print("Content(文本内容):"); 89 out.println(Content.getTextContent()); 90 out.print("MsgId(文件消息标识号):"); 91 out.println(MsgId.getTextContent()); 92 } catch (ParserConfigurationException e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 } catch (Exception e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 } 100 101 //将输入流InputStream变为String 102 public String readFromInputStream(InputStream in) throws IOException { 103 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 104 byte[] buffer = new byte[1024]; 105 int len = -1; 106 while ((len = in.read(buffer)) != -1) { 107 baos.write(buffer, 0, len); 108 } 109 baos.close(); 110 in.close(); 111 // 解析内容 看网上更改 输出中文乱码 但是未解决 已知输入流为gbk 输出为utf-8 求如何转换 112 byte[] lens = baos.toByteArray(); 113 String result = new String(lens,"UTF-8"); 114 115 return result; 116 117 } 118 }