3e73995141d2ad6aaf14c923f8dfdebc4981b64f.svn-base 598 Bytes
package com.phxl.common.util;

import com.thoughtworks.xstream.XStream;

public class XMLUtil {
    private static final XStream xStream = new XStream();

    // 将对象转为XML字符串
    public static <T> String toXML(T obj) {
        Class<?> cls = obj.getClass();
        xStream.alias(cls.getSimpleName().toLowerCase(), cls);
        xStream.aliasSystemAttribute(null, "class");
        return xStream.toXML(obj);
    }

    // 将XML字符串转为对象
    @SuppressWarnings({"unchecked"})
    public static <T> T fromXML(String xml) {
        return (T) xStream.fromXML(xml);
    }
}