xstream 实现simplebean2xml ,xml2simplebean
Posted mumian2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xstream 实现simplebean2xml ,xml2simplebean相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章