springboot XStream进行Bean和xml互转

很多时候我们不仅要解析json格式的数据,xml也是一种很常见的数据格式,很多老的系统都采用的这种方式,我们要与之通讯和交换数据就必须做数据转换,XStream很方便的实现了bean和xml的互转。
以下共享给大家一个我经常用到的一个方法类:

public class XStreamUtils {
    private static String XML_TAG = "";
    // 模板属性参数
    private static String PROFILE_ID = "profileId";
    private static String LOGO_ID = "logoId";
    private static String CREATE_TIME = "createTime";
    private static String MODIFY_TIME = "modifyTime";
    // 任务属性参数
    private static String TASK_ID = "taskId";
    private static String TASK_DEFAULT_INFO = "taskDefaultInfo";
    private static String INPUT_ID = "inputId";
    private static String VIDEO_ID = "videoId";
    private static String AUDIO_ID = "audioId";
    private static String ENCODE_ID = "encodeId";
    private static String PARAM_ID = "paramId";
    private static String OUTPUT_ID = "outputId";
    private static String PRETREAT_ID = "pretreatId";
    private static String SHIELD_ID = "shieldId";




    /**
     * Description: 私有化构造
     */
    private XStreamUtils() {
        super();
    }
    /**
     * @Description 为每次调用生成一个XStream
     * @Title getInstance
     * @return
     */
    private static XStream getInstance() {
        XStream xStream = new XStream(new DomDriver("UTF-8")) {
            /**
             * 忽略xml中多余字段
             */
            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    @SuppressWarnings("rawtypes")
                    @Override
                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        }
                        return super.shouldSerializeMember(definedIn, fieldName);
                    }
                };
            }
        };

        // 设置默认的安全校验
        XStream.setupDefaultSecurity(xStream);
        // 使用本地的类加载器
        xStream.setClassLoader(XStreamUtils.class.getClassLoader());
        // 允许所有的类进行转换
        xStream.addPermission(AnyTypePermission.ANY);
        return xStream;
    }

    /**
     * @Description 将xml字符串转化为java对象
     * @Title xmlToBean
     * @param xml
     * @param clazz
     * @return
     */
    public static  T xmlToBean(String xml, Class clazz) {
        XStream xStream = getInstance();
        xStream.processAnnotations(clazz);
        Object object = xStream.fromXML(xml);
        T cast = clazz.cast(object);
        return cast;
    }

    /**
     * @Description 将xml字符串转化为java对象
     * @Title xmlToBean
     * @param file
     * @param clazz
     * @return
     */
    public static Object xmlToBean(File file, Class clazz) {
        XStream xStream = getInstance();
        xStream.processAnnotations(clazz);
        // 忽略未知字段
        xStream.ignoreUnknownElements();

        Object object = xStream.fromXML(file);

        return object;
    }

    /**
     * @Description 将java对象转化为xml字符串
     * @Title beanToXml
     * @param object
     * @return
     */
    public static String beanToXml(Object object, Class clazz) {
        XStream xStream = getInstance();
        // 忽略对象参数
//        overLookParam(xStream);
        xStream.processAnnotations(clazz);
        // 剔除所有tab、制表符、换行符
//        String xml = xStream.toXML(object).replaceAll("\\s+", " ");
        String xml = xStream.toXML(object);
        return xml;
    }

    /**
     * @Description 将java对象转化为xml字符串(包含xml头部信息)
     * @Title beanToXml
     * @param object
     * @return
     */
    public static String beanToXmlWithTag(Object object, Class clazz) {
        String xml = XML_TAG + "\n" + beanToXml(object, clazz);
        return xml;
    }

    /**
     * 被忽略的字段
     * @param xStream
     */
    private static void overLookParam(XStream xStream) {
       /* // 模板参数
        xStream.omitField(ProfileVO.class, PROFILE_ID);
        xStream.omitField(ProfileLogo.class, LOGO_ID);
        xStream.omitField(ProfileLogo.class, PROFILE_ID);
        xStream.omitField(ProfileLogo.class, CREATE_TIME);
        xStream.omitField(ProfileLogo.class, MODIFY_TIME);

        // 任务参数
        xStream.omitField(TaskVO.class, TASK_ID);
        xStream.omitField(TaskVO.class, TASK_DEFAULT_INFO);

        xStream.omitField(TaskInputVO.class, INPUT_ID);
        xStream.omitField(TaskInputVO.class, TASK_ID);

        xStream.omitField(TaskInputVideo.class, VIDEO_ID);
        xStream.omitField(TaskInputVideo.class, INPUT_ID);
        xStream.omitField(TaskInputAudio.class, AUDIO_ID);
        xStream.omitField(TaskInputAudio.class, INPUT_ID);

        xStream.omitField(TaskEncodeVO.class, ENCODE_ID);
        xStream.omitField(TaskEncodeVO.class, TASK_ID);

        xStream.omitField(TaskEncodeParam.class, PARAM_ID);
        xStream.omitField(TaskEncodeParam.class, ENCODE_ID);
        xStream.omitField(TaskEncodeParam.class, CREATE_TIME);
        xStream.omitField(TaskEncodeParam.class, MODIFY_TIME);

        xStream.omitField(TaskEncodeLogo.class, LOGO_ID);
        xStream.omitField(TaskEncodeLogo.class, ENCODE_ID);
        xStream.omitField(TaskEncodeLogo.class, CREATE_TIME);
        xStream.omitField(TaskEncodeLogo.class, MODIFY_TIME);

        xStream.omitField(TaskOutput.class, OUTPUT_ID);
        xStream.omitField(TaskOutput.class, ENCODE_ID);
        xStream.omitField(TaskOutput.class, CREATE_TIME);
        xStream.omitField(TaskOutput.class, MODIFY_TIME);

        xStream.omitField(TaskPretreat.class, PRETREAT_ID);
        xStream.omitField(TaskPretreat.class, TASK_ID);

        xStream.omitField(TaskShield.class, SHIELD_ID);
        xStream.omitField(TaskShield.class, TASK_ID);*/

    }

}

更多,请关注:
springboot 技术实践总结

你可能感兴趣的:(springboot XStream进行Bean和xml互转)