Java tutorial
/** * Copyright (c) 2011-2014, James Zhan (jfinal@126.com). * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.xuan.sdk.msg; import com.xuan.sdk.msg.in.*; import com.xuan.sdk.msg.in.event.*; import com.xuan.sdk.msg.in.speech_recognition.InSpeechRecognitionResults; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.jfinal.kit.StrKit; public class InMsgParser { private InMsgParser() { } /** * xml ??? */ public static InMsg parse(String xml) { try { return doParse(xml); } catch (DocumentException e) { throw new RuntimeException(e); } } /** * ? * 1text ? * 2image ? * 3voice ? * 4video ? * 5location ??? * 6link ? * 7event */ private static InMsg doParse(String xml) throws DocumentException { Document doc = DocumentHelper.parseText(xml); Element root = doc.getRootElement(); String toUserName = root.elementText("ToUserName"); String fromUserName = root.elementText("FromUserName"); Integer createTime = Integer.parseInt(root.elementText("CreateTime")); String msgType = root.elementText("MsgType"); if ("text".equals(msgType)) return parseInTextMsg(root, toUserName, fromUserName, createTime, msgType); if ("image".equals(msgType)) return parseInImageMsg(root, toUserName, fromUserName, createTime, msgType); if ("voice".equals(msgType)) return parseInVoiceMsgAndInSpeechRecognitionResults(root, toUserName, fromUserName, createTime, msgType); if ("video".equals(msgType)) return parseInVideoMsg(root, toUserName, fromUserName, createTime, msgType); if ("location".equals(msgType)) return parseInLocationMsg(root, toUserName, fromUserName, createTime, msgType); if ("link".equals(msgType)) return parseInLinkMsg(root, toUserName, fromUserName, createTime, msgType); if ("event".equals(msgType)) return parseInEvent(root, toUserName, fromUserName, createTime, msgType); throw new RuntimeException( "? " + msgType + "??"); } private static InMsg parseInTextMsg(Element root, String toUserName, String fromUserName, Integer createTime, String msgType) { InTextMsg msg = new InTextMsg(toUserName, fromUserName, createTime, msgType); msg.setContent(root.elementText("Content")); msg.setMsgId(root.elementText("MsgId")); return msg; } private static InMsg parseInImageMsg(Element root, String toUserName, String fromUserName, Integer createTime, String msgType) { InImageMsg msg = new InImageMsg(toUserName, fromUserName, createTime, msgType); msg.setPicUrl(root.elementText("PicUrl")); msg.setMediaId(root.elementText("MediaId")); msg.setMsgId(root.elementText("MsgId")); return msg; } private static InMsg parseInVoiceMsgAndInSpeechRecognitionResults(Element root, String toUserName, String fromUserName, Integer createTime, String msgType) { String recognition = root.elementText("Recognition"); if (StrKit.isBlank(recognition)) { InVoiceMsg msg = new InVoiceMsg(toUserName, fromUserName, createTime, msgType); msg.setMediaId(root.elementText("MediaId")); msg.setFormat(root.elementText("Format")); msg.setMsgId(root.elementText("MsgId")); return msg; } else { InSpeechRecognitionResults msg = new InSpeechRecognitionResults(toUserName, fromUserName, createTime, msgType); msg.setMediaId(root.elementText("MediaId")); msg.setFormat(root.elementText("Format")); msg.setMsgId(root.elementText("MsgId")); msg.setRecognition(recognition); // InVoiceMsg ?? return msg; } } private static InMsg parseInVideoMsg(Element root, String toUserName, String fromUserName, Integer createTime, String msgType) { InVideoMsg msg = new InVideoMsg(toUserName, fromUserName, createTime, msgType); msg.setMediaId(root.elementText("MediaId")); msg.setThumbMediaId(root.elementText("ThumbMediaId")); msg.setMsgId(root.elementText("MsgId")); return msg; } private static InMsg parseInLocationMsg(Element root, String toUserName, String fromUserName, Integer createTime, String msgType) { InLocationMsg msg = new InLocationMsg(toUserName, fromUserName, createTime, msgType); msg.setLocation_X(root.elementText("Location_X")); msg.setLocation_Y(root.elementText("Location_Y")); msg.setScale(root.elementText("Scale")); msg.setLabel(root.elementText("Label")); msg.setMsgId(root.elementText("MsgId")); return msg; } private static InMsg parseInLinkMsg(Element root, String toUserName, String fromUserName, Integer createTime, String msgType) { InLinkMsg msg = new InLinkMsg(toUserName, fromUserName, createTime, msgType); msg.setTitle(root.elementText("Title")); msg.setDescription(root.elementText("Description")); msg.setUrl(root.elementText("Url")); msg.setMsgId(root.elementText("MsgId")); return msg; } // ?? private static InMsg parseInEvent(Element root, String toUserName, String fromUserName, Integer createTime, String msgType) { String event = root.elementText("Event"); String eventKey = root.elementText("EventKey"); // /??????????? if (("subscribe".equals(event) || "unsubscribe".equals(event)) && StrKit.isBlank(eventKey)) { InFollowEvent e = new InFollowEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); return e; } // ???? 1: ?? String ticket = root.elementText("Ticket"); if ("subscribe".equals(event) && StrKit.notBlank(eventKey) && eventKey.startsWith("qrscene_")) { InQrCodeEvent e = new InQrCodeEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); e.setEventKey(eventKey); e.setTicket(ticket); return e; } // ???? 2: ? if ("SCAN".equals(event)) { InQrCodeEvent e = new InQrCodeEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); e.setEventKey(eventKey); e.setTicket(ticket); return e; } // ?? if ("LOCATION".equals(event)) { InLocationEvent e = new InLocationEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); e.setLatitude(root.elementText("Latitude")); e.setLongitude(root.elementText("Longitude")); e.setPrecision(root.elementText("Precision")); return e; } // ?? 1????? if ("CLICK".equals(event)) { InMenuEvent e = new InMenuEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); e.setEventKey(eventKey); return e; } // ?? 2??? if ("VIEW".equals(event)) { InMenuEvent e = new InMenuEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); e.setEventKey(eventKey); return e; } // ????? if ("TEMPLATESENDJOBFINISH".equals(event)) { InTemplateMsgEvent e = new InTemplateMsgEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); e.setMsgId(root.elementText("MsgID")); e.setStatus(root.elementText("Status")); return e; } // ????? if ("MASSSENDJOBFINISH".equals(event)) { InMassEvent e = new InMassEvent(toUserName, fromUserName, createTime, msgType); e.setEvent(event); e.setMsgId(root.elementText("MsgID")); e.setStatus(root.elementText("Status")); e.setTotalCount(root.elementText("TotalCount")); e.setFilterCount(root.elementText("FilterCount")); e.setSentCount(root.elementText("SentCount")); e.setErrorCount(root.elementText("ErrorCount")); return e; } throw new RuntimeException( "" + event + "??"); } @SuppressWarnings("unused") public static void main(String[] args) throws DocumentException { String xml = "<xml>\n" + "<ToUserName><![CDATA[James]]></ToUserName>\n" + "<FromUserName><![CDATA[JFinal]]></FromUserName>\n" + "<CreateTime>1348831860</CreateTime>\n" + "<MsgType><![CDATA[text]]></MsgType>\n" + "<Content><![CDATA[this is a test]]></Content>\n" + "<MsgId>1234567890123456</MsgId>\n" + "</xml>"; // InTextMsg msg = (InTextMsg)parse(xml); // System.out.println(msg.getToUserName()); // System.out.println(msg.getFromUserName()); // System.out.println(msg.getContent()); String xml_2 = "<xml>\n" + "<ToUserName><![CDATA[James]]></ToUserName>\n" + "<FromUserName><![CDATA[JFinal]]></FromUserName>\n" + "<CreateTime>1348831860</CreateTime>\n" + "<MsgType><![CDATA[text]]></MsgType>\n" + "<Content><![CDATA[this is a test]]></Content>\n" + "<MsgId>1234567890123456</MsgId>\n" + "</xml>"; Document doc = DocumentHelper.parseText(xml_2); Element root = doc.getRootElement(); String value = root.elementText("abc"); System.out.println(value); } }