Java Jaxb JavaBean与XML互转
1.Jaxb - Java Arcitecture for XML Binding
是业界的一个标准,是一项能够依据XML Schema产生Java类的技术。
Jaxb2.0是Jdk1.6的组成部分。不须要在第三方Jar包的支持下就可以完毕Xml与JavaBean的相互转换。
2.重要概念:
·JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。
·Marshaller接口,将Java对象序列化为XML数据。
·Unmarshaller接口,将XML数据反序列化为Java对象。
·@XmlType,将Java类或枚举类型映射到XML模式类型
·@XmlAccessorType(XmlAccessType.FIELD),控制字段或属性的序列化。FIELD表示JAXB将自己主动绑定JAVA类中的每一个非静态,非瞬时(由@XmlTransient标注)字段到
XML。其它值还有XmlAccessType.PROPERTY和XmlAccessType.NONE.
·@XmlAccessorOrder。控制Jaxb绑定类中属性和字段的排序。
·XmlJavaTypeAdapter。使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法)。以序列化Java类为Xml
·@XmlElementWrapper,对于数组和集合(即包括多个元素的成员变量)。生成一个包装该数组和结合的XML元素(称为包装器)
·@XmlRootElement。将Java类或枚举类型映射到XML元素。
·@XmlElement。将Java类中的一个属性映射到与属性同名的一个XML元素。
·@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。
3.应用:
对于Jaxb的使用是很easy的,例如以下是本人写的一个代码,通过Util类完毕JavaBean和Xml之间的转换
JaxbUtil类:负责与Jaxb的中转:
public class JaxbUtil {
public static String convertToXml(Object obj) {
return convertToXml(obj, "UTF-8");
}
/*** @note JavaBean 转 xml
* @param obj
* JavaBean
* @param encoding
* 编码
* @return xml
*/
public static String convertToXml(Object obj, String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception ex) {
}
return result;
}/**
* @note xml 转 JavaBean
* @param xml
* xml
* @param c
* JavBean
* @return
*/
public static <T> T convertToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception ex) {
ex.printStackTrace();
}
return t;
}
}
Simple类:简单的Bean对象,当中main方法是对它的測试
@XmlAccessorType(XmlAccessType.FIELD)@XmlRootElement
@XmlType(name = "boook", propOrder = { "author", "calendar", "price", "id" })public class Simple {
@XmlElement(required = true)
private String author;@XmlElement(name="price_l", required=true)
private float price;
@XmlElement
private Date calendar;
@XmlElement
private Integer id;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {this.price = price;
}
public Date getCalendar() {return calendar;
}
public void setCalendar(Date calendar) {this.calendar = calendar;
}
public Integer getId() {return id;
}
public void setId(Integer id) {this.id = id;
}public static void main(String[] agrs){
Simple simple = new Simple();simple.setId(100);
simple.setAuthor("wangbc");simple.setCalendar(new Date());
simple.setPrice(23.45f);String str = JaxbUtil.convertToXml(simple);
System.out.println(str);
}
}
Collect类:比較复杂的Bean对象,当中main方法是对它的測试
@XmlAccessorType(XmlAccessType.FIELD)@XmlRootElement(name = "hard")
@XmlType(propOrder = { "name", "provinceList" })
public class Collect {@XmlElement(name = "country_name")
private String name;
@XmlElementWrapper(name = "provinces")
@XmlElement(name = "province")
private List<Province> provinceList;
return name;
}
public void setName(String name) {this.name = name;
}
public List<Province> getProvinceList() {return provinceList;
}
public void setProvinceList(List<Province> provinceList) {this.provinceList = provinceList;
}
public static void main(String[] args){
Collect con = new Collect ();
con.setName("中国");
List<Province> list = new ArrayList<Province>();
Province pro = new Province();
pro.setName("江苏省");
pro.setProvCity("南京市");
Province pro2 = new Province();
pro2.setName("浙江省");pro2.setProvCity("杭州市");
list.add(pro);
list.add(pro2);
con.setProvinceList(list);
String str = JaxbUtil.convertToXml(con);
System.out.println(str);}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "name", "provCity" })
class Province {
@XmlElement(name = "province_name")
private String name;
@XmlElement(name = "prov_city")
private String provCity;
return name;
}
public void setName(String name) {this.name = name;
}
public String getProvCity() {return provCity;
}
public void setProvCity(String provCity) {this.provCity = provCity;
}
}如上即完毕了JavaBean与XML之间的转换操作,Jaxb使用相对还是比較简单的,并且Jaxb不须要引入第三方的jar进行依赖。实现全然由jdk负责。
posted on 2017-04-20 21:12 gavanwanggw 阅读(240) 评论(0) 编辑 收藏 举报