首页 > 代码库 > Java反射机制实现微信公众平台XML数据解析
Java反射机制实现微信公众平台XML数据解析
在做微信公众平台开发时,接收消息中有很多种类型的xml消息,比如“文本消息”“图片消息”“语音消息”“视频消息”“链接消息”“地理位置消息”等等,最初在开发时针对每一种数据格式写了一个实体类,单独做解析,当然这样写的好处是层次分明。但经过尝试之后,用一个统一的实体类消息做处理,更加高效,当然,里面最核心的就是在遍历xml数据时,通过反射的机制动态调用set方法:
1、ReceiveXmlEntity.java实体类。包含了所有的接收到的xml字段
package com.basicwechat.entity; /** * 接收到的微信xml实体类 * @author pamchen-1 * */ public class ReceiveXmlEntity { private String ToUserName=""; private String FromUserName=""; private String CreateTime=""; private String MsgType=""; private String MsgId=""; private String Event=""; private String EventKey=""; private String Ticket=""; private String Latitude=""; private String Longitude=""; private String Precision=""; private String PicUrl=""; private String MediaId=""; private String Title=""; private String Description=""; private String Url=""; private String Location_X=""; private String Location_Y=""; private String Scale=""; private String Label=""; private String Content=""; private String Format=""; private String Recognition=""; public String getRecognition() { return Recognition; } public void setRecognition(String recognition) { Recognition = recognition; } public String getFormat() { return Format; } public void setFormat(String format) { Format = format; } public String getContent() { return Content; } public void setContent(String content) { Content = content; } public String getLocation_X() { return Location_X; } public void setLocation_X(String locationX) { Location_X = locationX; } public String getLocation_Y() { return Location_Y; } public void setLocation_Y(String locationY) { Location_Y = locationY; } public String getScale() { return Scale; } public void setScale(String scale) { Scale = scale; } public String getLabel() { return Label; } public void setLabel(String label) { Label = label; } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public String getUrl() { return Url; } public void setUrl(String url) { Url = url; } public String getPicUrl() { return PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } public String getMediaId() { return MediaId; } public void setMediaId(String mediaId) { MediaId = mediaId; } public String getEventKey() { return EventKey; } public void setEventKey(String eventKey) { EventKey = eventKey; } public String getTicket() { return Ticket; } public void setTicket(String ticket) { Ticket = ticket; } public String getLatitude() { return Latitude; } public void setLatitude(String latitude) { Latitude = latitude; } public String getLongitude() { return Longitude; } public void setLongitude(String longitude) { Longitude = longitude; } public String getPrecision() { return Precision; } public void setPrecision(String precision) { Precision = precision; } public String getEvent() { return Event; } public void setEvent(String event) { Event = event; } public String getMsgId() { return MsgId; } public void setMsgId(String msgId) { MsgId = msgId; } public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public String getCreateTime() { return CreateTime; } public void setCreateTime(String createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } }2、ParseReceiveXml.hava 通过反射的机制动态调用set方法,将xml消息转成实体类,避免大批量的if重复判断。
package com.basicwechat.parse; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.basicwechat.entity.ReceiveXmlEntity; /** * 解析接收到的微信xml,返回消息对象 * @author pamchen-1 * */ public class ParseReceiveXml { /** * 解析微信xml消息 * @param strXml * @return */ public ReceiveXmlEntity getMsgEntity(String strXml){ ReceiveXmlEntity msg = null; try { if (strXml.length() <= 0 || strXml == null) return null; // 将字符串转化为XML文档对象 Document document = DocumentHelper.parseText(strXml); // 获得文档的根节点 Element root = document.getRootElement(); // 遍历根节点下所有子节点 Iterator<?> iter = root.elementIterator(); // 遍历所有结点 msg = new ReceiveXmlEntity(); //利用反射机制,调用set方法 //获取该实体的元类型 Class<?> c = Class.forName("com.basicwechat.entity.ReceiveXmlEntity"); msg = (ReceiveXmlEntity)c.newInstance();//创建这个实体的对象 while(iter.hasNext()){ Element ele = (Element)iter.next(); //获取set方法中的参数字段(实体类的属性) Field field = c.getDeclaredField(ele.getName()); //获取set方法,field.getType())获取它的参数数据类型 Method method = c.getDeclaredMethod("set"+ele.getName(), field.getType()); //调用set方法 method.invoke(msg, ele.getText()); } } catch (Exception e) { // TODO: handle exception System.out.println("xml 格式异常: "+ strXml); e.printStackTrace(); } return msg; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。