注解+代码
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
注解:
@XStreamAlias("a")
代码:
public <T> T transferXMLToBean(Class<T> clazz, String str) {
XStream xstream = new XStream();
xstream.processAnnotations(clazz);
xstream.autodetectAnnotations(true);
xstream.setupDefaultSecurity(xstream);
xstream.allowTypes(new Class[]{clazz});
//或// xstream.allowTypesByRegExp(new String[] {".*"});
xstream.setClassLoader(clazz.getClassLoader());//这行是重点
return (T)xstream.fromXML(str);
}