JavaBean与xml相互转化解决办法

JAVA实用技巧

2017-02-17

34

0

一种简单的xml转化为javabean与javabean转化为xml

使用的框架为:xstream地址,下载JAR包 xstream-1.4.9.jar

代码如下

package com.extr.util.xml;

import java.util.Date;

import com.extr.domain.exam.Exam;

import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.io.xml.DomDriver;


public class Object2Xml {
   public static String toXml(Object obj){
   XStream xstream=new XStream();
   xstream.processAnnotations(obj.getClass());
   return xstream.toXML(obj);
}

public static  T toBean(String xmlStr,Class cls){
   XStream xstream=new XStream(new DomDriver());
   xstream.processAnnotations(cls);
   @SuppressWarnings("unchecked")
   T obj=(T)xstream.fromXML(xmlStr);
   return obj;
}

}

发表评论

全部评论:0条

houzhe11

热爱技术、热爱开源