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的使用是非常简单的,如下是本人写的一个代码,通过Util类完成JavaBean和Xml之间的转换
JaxbUtil类:负责与Jaxb的中转:
public class JaxbUtil {
public static String convertToXml(Object obj) {
return convertToXml(obj, "UTF-8");
}
/**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;
}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;
}
public void setName(String name) {this.name = name;
}
public String getProvCity() {return provCity;
}
public void setProvCity(String provCity) {this.provCity = provCity;
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/securitit/article/details/47010183