Xstream xml与bean转换工具类

情到浓时终转凉″ 提交于 2020-01-09 11:36:12

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NameCoder;
import com.thoughtworks.xstream.io.xml.Xpp3DomDriver;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;

/**
 * xstream工具封装
 * 用以处理xml与bean的转换
 */
public class XstreamUtils {

    /**
     * 在xml中多余的节点生成bean时会抛出异常
     * 通过该mapperWrapper跳过不存在的属性
     *
     * @param mapper
     * @return MapperWrapper [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    private static MapperWrapper createSkipOverElementMapperWrapper(
            Mapper mapper) {
        MapperWrapper resMapper = new MapperWrapper(mapper) {
            /**
             * @param elementName
             * @return
             */
            @SuppressWarnings("rawtypes")
            @Override
            public Class realClass(String elementName) {
                Class res = null;
                try {
                    res = super.realClass(elementName);
                } catch (CannotResolveClassException e) {
                    // logger.error(e.getMessage(),e);
                    // logger.info(Thread.currentThread().getStackTrace()[1].getMethodName()+"  xstream change xml to object. filed (0) not exsit. "+elementName);
                }
                return res;
            }
        };

        return resMapper;
    }

    /**
     * 获取xstream转换对象
     *
     * @param classType
     * @return XStream [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static XStream getXstream(Class<?> classType) {
        return getXstream(classType, true);
    }

    /**
     * 获取xstream转换对象
     *
     * @param classType
     * @param isSkipOverElement
     * @return XStream [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static XStream getXstream(Class<?> classType,
                                     boolean isSkipOverElement) {
        NameCoder nameCoder = new NameCoder() {
            @Override
            public String encodeNode(String arg0) {
                return arg0;
            }

            @Override
            public String encodeAttribute(String arg0) {
                return arg0;
            }

            @Override
            public String decodeNode(String arg0) {
                return arg0;
            }

            @Override
            public String decodeAttribute(String arg0) {
                return arg0;
            }
        };
        XStream res = null;
        if (isSkipOverElement) {
            res = new XStream(new Xpp3DomDriver(nameCoder)) {
                /**
                 * @param next
                 * @return
                 */
                @Override
                protected MapperWrapper wrapMapper(MapperWrapper next) {
                    return createSkipOverElementMapperWrapper(next);
                }
            };
        } else {
            res = new XStream(new Xpp3DomDriver(nameCoder));
        }
        res.processAnnotations(classType);

        return res;
    }

} 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!