当前位置 : 主页 > 编程语言 > java >

xml解析整理,转化为对象

来源:互联网 收集:自由互联 发布时间:2021-06-28
XmlToBeanUtil import com.thoughtworks.xstream.XStream;/** * @Description: 通过XStream对XML对象转化工具类 * @Author: nobody * @Time: 2017年6月18日 */public class XMLUtil {private static final XStream xStream = new XStream();// o
XmlToBeanUtil
import com.thoughtworks.xstream.XStream;

/**
 * @Description: 通过XStream对XML对象转化工具类
 * @Author: nobody
 * @Time: 2017年6月18日
 */
public class XMLUtil {
	private static final XStream xStream = new XStream();

	// obj2xml
	public static 
 
   String toXML(T obj) {
		Class
   cls = obj.getClass();
		xStream.alias(cls.getSimpleName().toLowerCase(), cls);
		xStream.aliasSystemAttribute(null, "class"); 
		return xStream.toXML(obj);
	}

	// xml2obj
	@SuppressWarnings("unchecked")
	public static 
  
    T fromXML(String xml,Class
    clazz) { xStream.alias(clazz.getSimpleName().toLowerCase(), clazz); xStream.aliasSystemAttribute(null, "class"); return (T)xStream.fromXML(xml); } }
  
 
网友评论