javabean与xml相互转换
java bean 与xml相互转换的⽅式有多种。这⾥介绍jaxb2.0和xstream的⽅式。⾸先需要引⼊xstream和jaxb2.0的jar包
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId&l.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.3</version>
</dependency>
xstream⽅式
1. 实体类定义
1.1Book.java
public class Book {
@XStreamAlias("Name")//通过注解的⽅式重定义节点名称
private String name;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
1.2Address.java
@XStreamAlias("Address")
public class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
1.3Student.java
public class Student {
@XStreamAlias("Name")
private String name;
//⾃定义的类型转换器
@XStreamConverter(value=MyDateConverter.class,strings={"yyyyMMddHHmmss"})
@XStreamAlias("Birth")
private Date birthday;
@XStreamAlias("Address")
private Address address;
@XStreamAlias("books")
private List<Book> list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<Book> getList() {
return list;
}
public void setList(List<Book> list) {
this.list = list;
}
}
2. ⾃定义类型转换器
/**
* 通过注解对不同Date格式的⽀持
* <p/>
* XStream中DateConverter,它默认⽀持的格式是:yyyy-MM-dd HH:mm:ss.S 'UTC'
* 通过@XStreamConverter注解可以定义⾃⼰的Date格式
* <p/>
* DateConverter(String defaultFormat, String[] acceptableFormats, TimeZone timeZone)
* Construct a DateConverter with a given TimeZone
*/
public class MyDateConverter extends DateConverter {
public MyDateConverter(String dateFormat) {
super(dateFormat, new String[]{dateFormat}, TimeZone("GMT+8"));
}
}
3. xstream 转换⼯具类
⼯具类实现bean 和xml 之间相互转换
import com.ity.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.l.StaxDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
bigdecimal转换为integerimport java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
public abstract class XStreamUtil {
private final static Logger logger = Logger(XStreamUtil.class);
private final static StaxDriver domDriver = new StaxDriver();
private final static XStream o2x = new XStream(domDriver);
private final static XStream x2o = new XStream(domDriver);
private final static void initO2X() {
List<Class> basic = new ArrayList(3);
//需要把⽤到的实体类都添加,否则不会使⽤注解⽅式来定义别名,但是这样的⽅式不好。代码侵⼊⼤        basic.add(Student.class);
basic.add(Address.class);
basic.add(Book.class);
Class[] clazzs = new Class[basic.size()];
设置使⽤注解⽅式来处理别名信息
o2x.Array(clazzs));
}
private final static void initX2O() {
List<Class> basic = new ArrayList(3);
basic.add(Student.class);
basic.add(Address.class);
basic.add(Book.class);
Class[] clazzs = new Class[basic.size()];
x2o.Array(clazzs));
}
static {
initO2X();
initX2O();
}
/**
* 实体类转xml字符串
* 实体类转xml字符串
* @param obj 实体类
* @return
* @throws Exception
*/
public static String object2Xml(Object obj) throws Exception {
if (null == obj) {
return null;
}
StringWriter writer = new StringWriter();
String();
}
/**
*
* xml字符串转实体类
* @param xml 字符串
* @param clazz 实体类
* @return
*/
public static Object xml2Object(String xml, Class<?> clazz) {
if (null == xml || "".im())) {
return null;
}
Object obj = null;
try {
obj = x2o.fromXML(xml, wInstance());
} catch (Exception e) {
<(e.getMessage(), e);
}
return obj;
}
}
jaxb2.0⽅式
1. 实体类定义
1.1Summary.java
l.bind.annotation.XmlElement;
l.bind.annotation.XmlRootElement;
l.bind.annotation.XmlType;
@XmlRootElement(name = "Message")//表⽰该类是XML根元素  name属性定义了XML根元素的元素名
@XmlType(propOrder = { "authInfo", "summaryDetail" })
//定义XML的⽣成规则 propOrder 属性在xml⾥的出现顺序
public class Summary {
private AuthInfo authInfo;
private SummaryDetail summaryDetail;
@XmlElement(name = "AuthInfo")//定义⽣成后的xml的属性名,这个和xstream区别是这个写在get⽅法上public AuthInfo getAuthInfo() {
return authInfo;
}
public void setAuthInfo(AuthInfo authInfo) {
this.authInfo = authInfo;
}
@XmlElement(name = "Detail")
public SummaryDetail getSummaryDetail() {
return summaryDetail;
}
public void setSummaryDetail(SummaryDetail summaryDetail) {
this.summaryDetail = summaryDetail;
}
}
1.2 AuthInfo.java
l.bind.annotation.XmlElement;
l.bind.annotation.XmlType;
@XmlType(propOrder = { "username", "password"})//节点显⽰的顺序
public class AuthInfo {
private String username;
private String password;
@XmlElement(name = "UserName")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@XmlElement(name = "Password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
1.3 SummaryDetail.java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。