xml和对象互转工具类


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.DocumentException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class XmlBeanServer {
    /**
     * 转换器Map
     */
    private static final ConcurrentHashMap, XStream> converterMap = new ConcurrentHashMap, XStream>();

    /**
     * 同步锁
     */
    private static final Object lock = new Object();

    /**
     * 将XML报文转换成Bean对象
     *
     * @param xml
     * @return
     * @see [类、类#方法、类#成员]
     */
      public static  T xmlToBean(Class clazz, String xml) {
        return (T) getConverter(clazz).fromXML(xml);
    }

    /**
     * 将Bean对象转换成XML报文
     *
     * @param bean
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String beanToXml(Object bean) {
        return getConverter(bean.getClass()).toXML(bean).replaceAll("__", "_");
    }

    /**
     * 根据Bean class生成对应的转换器
     *
     * @param clazz
     * @return
     * @see [类、类#方法、类#成员]
     */
    private static XStream getConverter(Class clazz) {
        XStream con = converterMap.get(clazz);
        if (null == con) {
            synchronized (lock) {
                con = converterMap.get(clazz);
                if (null == con) {
                    con = new XStream(new DomDriver());
                    con.processAnnotations(clazz);
                    converterMap.put(clazz, con);
                }
            }
        }
        con.alias(clazz.getSimpleName(), clazz);
        con.alias("GoodsInfo", GoodsInfo.class);
        con.alias("GoodsResInfo", GoodsResInfo.class);
        return con;
    }

     public static String regexLabel(String xml, String label) {
        String context = "";
        //正则表达式
        String regex = "<" + label + ">(.*?)";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(xml);
        List list = new ArrayList();
        while (m.find()) {
            int i = 1;
            list.add(m.group(i));
            i++;
        }
        if (list.size() > 0) {
            context = list.get(0);
        }
        return "<" + label + ">" + context + "";
    }

    
	/**
     * 将XML报文转换成复杂Bean对象
     *
     * @param xml
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static  T convertXmlToBean(Class clazz, String xml) {
        XStream con = converterMap.get(clazz);
        if (null == con) {
            synchronized (lock) {
                con = converterMap.get(clazz);
                if (null == con) {
                    con = new XStream(new DomDriver());
                    con.processAnnotations(clazz);
                    converterMap.put(clazz, con);
                }
            }
        }
		
        con.alias(clazz.getSimpleName(), clazz);
        con.alias("WbandInfoByTelNewModelBean", WbandInfoByTelNewModelBean.class);
        con.alias("joinacctInfo", JoinacctInfoBean.class);
        con.alias("AcctsubsListBean", AcctsubsListBean.class);
        return (T) con.fromXML(xml);
    }
}

 

你可能感兴趣的:(java)