XMLObjDigester-解析类

package com.vtradex.webservice.model;

import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

import com.sun.xml.messaging.saaj.util.ByteInputStream;
import com.vtradex.webservice.helper.XMLHelper;

/**
 * 
 * @Description:     解析类
 * @Author :      yc.min              
 * @Date :    2013-9-25
 */
public class XMLObjDigester {

    protected Log logger = LogFactory.getLog(getClass());
    
    public Object getObjectTemplate(String xml,Object obj,
		String methodName) {
        DataInputStream dis = null;
        try {
            byte[] xmls = xml.getBytes("UTF-8");
            dis = new DataInputStream(
				new ByteInputStream(xmls, 0, xmls.length));
            this.getDigester(obj,methodName).parse(dis);
        } catch (IOException e) {
            logger.error("", e);
        } catch (SAXException e) {
            logger.error("", e);
        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    logger.error("", e);
                }
            }
        }
        return obj;
    }
    
    @SuppressWarnings("rawtypes")
	private Digester getDigester(Object obj,
		String methodName) {
        Digester digester = new Digester();
        digester.push(obj);
        Class[] class1 = new Class[]{Digester.class} ;
		Object[] objects = new Object[]{digester};
		try {
			obj.getClass().getMethod(methodName, class1)
				.invoke(obj, objects);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
        return digester;
    }
    
}
--------------------------------------方法调用参考
//解析方法入口
    public void parseToTempleate(String xml) {
        new XMLObjDigester().getObjectTemplate(xml, 
			this,"parseConfig");
    }
    /**
     * 报文匹配规则
     */
    public void parseConfig(Digester digester) {
        digester.addObjectCreate("ZM/HEAD", 
			ZmHeadTemplate.class);
        digester.addBeanPropertySetter("ZM/HEAD/TYPE",
			"type");
        digester.addBeanPropertySetter("ZM/HEAD/SENDER",
			"sender");
        digester.addBeanPropertySetter("ZM/HEAD/RECEIVER",
			"receiver");
        digester.addBeanPropertySetter("ZM/HEAD/MESSAGEID",
			"messageid");
        digester.addBeanPropertySetter("ZM/HEAD/DTSEND",
			"dtsend");
        digester.addSetNext("ZM/HEAD", "setHead");
        
        digester.addObjectCreate("ZM/DATA", 
			ZmSmallListRowsTemplate.class);
        digester.addSetProperties("ZM/DATA");
        digester.addSetNext("ZM/DATA", 
			"setData");
        
        digester.addObjectCreate("ZM/DATA/ROW", 
			ZmSmallListRowTemplate.class);
        digester.addBeanPropertySetter("ZM/DATA/ROW/XH","xh");
        digester.addBeanPropertySetter("ZM/DATA/ROW/ID","id");
        digester.addBeanPropertySetter("ZM/DATA/ROW/SMALL_CPBH",
			"small_cpbh");   
        digester.addSetNext("ZM/DATA/ROW", 
			"addZmSmallListRowTemplate");  
        
    }

 

你可能感兴趣的:(xml)