码迷,mamicode.com
首页 > 其他好文 > 详细

xstream 实现simplebean2xml ,xml2simplebean

时间:2018-11-17 13:19:34      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:row   alias   exception   work   stat   tutorial   runtime   xstream   ebe   

xstream 实现simplebean2xml ,xml2simplebean

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
public class WSUtils {
      /**
     * xml转bean
     * @param alias
     * @param clazz
     * @param xml
     * @param <T>
     * @return
     */
    public static <T> T toBean(String alias,Class<T> clazz, String xml) {
        try {
            XStream xstream = new XStream(new StaxDriver());
            xstream.alias(alias, clazz);
            return (T) xstream.fromXML(xml);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("[XStream]XML转对象出错");
        }
    }

    /**
     * bean转xml
     * @param alias
     * @param clazz
     * @param obj
     * @param <T>
     * @return
     */
    public static <T> String toxml(String alias,Class<T> clazz, Object obj) {
        try {
            XStream xstream = new XStream(new StaxDriver());
            xstream.alias(alias, clazz);
            return  xstream.toXML((T)obj);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("[XStream]对象转XML出错");
        }
    }

复杂的参考官方文档
http://x-stream.github.io/tutorial.html

xstream 实现simplebean2xml ,xml2simplebean

标签:row   alias   exception   work   stat   tutorial   runtime   xstream   ebe   

原文地址:https://www.cnblogs.com/mumian2/p/9973348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!