首页 > 代码库 > 微信消息处理JAXP-dom解析
微信消息处理JAXP-dom解析
1 package cn.lihainan; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.PrintWriter; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.http.HttpServlet;10 import javax.servlet.http.HttpServletRequest;11 import javax.servlet.http.HttpServletResponse;12 import javax.xml.parsers.DocumentBuilder;13 import javax.xml.parsers.DocumentBuilderFactory;14 import javax.xml.parsers.ParserConfigurationException;15 16 import org.w3c.dom.Document;17 import org.w3c.dom.Node;18 import org.xml.sax.SAXException;19 20 public class Get extends HttpServlet {21 22 public void doPost(HttpServletRequest request, HttpServletResponse response)23 throws ServletException, IOException {24 25 try {26 request.setCharacterEncoding("UTF-8"); 27 //response.setCharacterEncoding("UTF-8");28 29 response.setContentType("textml");30 InputStream inputStream = request.getInputStream();31 32 33 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();34 DocumentBuilder builder;35 builder = factory.newDocumentBuilder();36 Document document = builder.parse(inputStream);37 38 39 System.out.println(document.getInputEncoding());40 41 Node ToUserName = document.getElementsByTagName("ToUserName").item(0); 42 System.out.println(ToUserName.getTextContent());43 44 Node FromUserName = document.getElementsByTagName("FromUserName").item(0); 45 System.out.println(FromUserName.getTextContent());46 47 Node MsgType= document.getElementsByTagName("MsgType").item(0); 48 System.out.println(MsgType.getTextContent());49 50 Node Content = document.getElementsByTagName("Content").item(0); 51 System.out.println(Content.getTextContent());52 53 Node MsgId= document.getElementsByTagName("MsgId").item(0); 54 System.out.println(MsgId.getTextContent());55 56 57 58 StringBuffer out1 = new StringBuffer();59 byte[] b = new byte[4096];60 for (int n; (n = inputStream.read(b)) != -1;) {61 out1.append(new String(b, 0, n));62 }63 String value=http://www.mamicode.com/out1.toString();64 65 66 PrintWriter out = response.getWriter();67 68 out.println(document.getInputEncoding());69 out.println("FromUserName(来自):");70 out.println(FromUserName.getTextContent());71 out.println("ToUserName(发送至):");72 out.println(ToUserName.getTextContent());73 out.println("CreateTime(时间):");74 out.println("MsgType(消息类型):");75 out.println(MsgType.getTextContent());76 out.println("Content(文本内容):");77 out.println(Content.getTextContent());78 out.println("MsgId(文件消息标识号):");79 out.println(MsgId.getTextContent());80 } catch (ParserConfigurationException e) {81 // TODO Auto-generated catch block82 e.printStackTrace();83 } catch (Exception e) {84 // TODO Auto-generated catch block85 e.printStackTrace();86 }87 }88 89 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。