All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.frameworkset.wx.common.util.ReceiveXmlUtil Maven / Gradle / Ivy

There is a newer version: 5.6.4
Show newest version
/**
 * 
 */
package org.frameworkset.wx.common.util;

import org.frameworkset.wx.common.entity.WxOrderResult;
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;

/**
 * @author suwei
 * @date 2016年10月27日
 *
 */
public class ReceiveXmlUtil {
	/**
	 * 解析微信xml消息
	 * 
	 * @param strXml
	 * @return
	 */
	public static WxOrderResult getMsgEntity(String strXml) {
		WxOrderResult 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 WxOrderResult();
			// 利用反射机制,调用set方法
			// 获取该实体的元类型
			Class c = Class.forName("org.frameworkset.wx.common.entity.WxOrderResult");
			msg = (WxOrderResult) c.newInstance();// 创建这个实体的对象

			while (iter.hasNext()) {
				Element ele = (Element) iter.next();
				// 获取set方法中的参数字段(实体类的属性)
				String fieldName = WXHelper.getBeanName(ele.getName());
				Field field = c.getDeclaredField(fieldName);
				// 获取set方法,field.getType())获取它的参数数据类型
				Method method = c.getDeclaredMethod("set" + WXHelper.captureName(fieldName), field.getType());
				// 调用set方法
				method.invoke(msg, ele.getText());
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("xml 格式异常: " + strXml);
			e.printStackTrace();
		}
		return msg;
	}

	public static Object getObjectEntity(Class z, Object obj, String strXml) {
		// WxOrderResult 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 WxOrderResult();
			// 利用反射机制,调用set方法
			// 获取该实体的元类型
			// Class c =
			// Class.forName("org.frameworkset.wx.common.entity.WxOrderResult");
			obj = z.newInstance();// 创建这个实体的对象

			while (iter.hasNext()) {
				Element ele = (Element) iter.next();
				// 获取set方法中的参数字段(实体类的属性)
				String fieldName = WXHelper.getBeanName(ele.getName());
				Field field = z.getDeclaredField(fieldName);
				// 获取set方法,field.getType())获取它的参数数据类型
				Method method = z.getDeclaredMethod("set" + WXHelper.captureName(fieldName), field.getType());
				// 调用set方法
				method.invoke(obj, ele.getText());
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("xml 格式异常: " + strXml);
			e.printStackTrace();
		}
		return obj;
	}

	public static void main(String args[]) {
		StringBuffer xml = new StringBuffer();
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("\n");
		xml.append("");
		getMsgEntity(xml.toString());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy