一、前言
JAXB——Java Architecture for XML Binding,是一項可以根據XML Schema產生Java類的技術。JAXB提供將XML實例文檔反向生成Java對象樹的方法,也能將Java對象樹的內容重新寫到XML實例文檔。
二、JAXB相關的class和interface
(1)JAXBContext。 JAXBContext類提供到 JAXB API 的客戶端入口點。它提供了管理實現 JAXB 綁定框架操作所需的 XML/Java 綁定信息的抽象,這些操作包括:解組(Unmarshaller )、編組(Marshaller)和驗證(Validator)。通常使用JAXBContext.newInstance(XXX.class)
來獲取JAXBContext實例(Student是我定義的一個Entity)。
JAXBContext ctx = JAXBContext.newInstance(Student.class)
(2)Unmarshaller。 Unmarshaller 是一個Interface,它管理將 XML 數據反序列化為新創建的 Java 內容樹的過程,並可在解組時有選擇地驗證 XML 數據。它針對如File,InputStream,URL,StringBuffer等各種不同的輸入種類,提供各種重載的 unmarshal 方法。unmarshal 方法從不返回 null。如果unmarshal無法將 XML 內容的根解組到 JAXB 映射對象,則拋出 JAXBException。
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Student stu = (Student) unmarshaller.unmarshal(file);
(3)Marshaller。Marshaller
使客戶端應用程序能夠將 Java 內容樹轉換回 XML 數據。它提供了各種重載的marshal方法。默認情況下,在將 XML 數據生成到 java.io.OutputStream 或 java.io.Writer 中時,Marshaller 將使用 UTF-8 編碼。
Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 格式化輸出 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 設置輸出編碼,默認為UTF-8 marshaller.marshal(stu, xmlFile);
三、JAXB相關Annotation
(1)@XmlRootElement,將Java類或枚舉類型映射到XML元素;
(2)@XmlElement,將Java類的一個屬性映射到與屬性同名的一個XML元素;
(3)@XmlAttribute,將Java類的一個屬性映射到與屬性同名的一個XML屬性;
注意事項:
(1)對於要序列化(marshal)為XML的Java類,絕不能把成員變量聲明為public,否則運行將拋出異常
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException
(2)注解不能直接放在成員變量上,可以放在成員變量的getter或setter方法上,任選其一,否則也會拋出IllegalAnnotationsException異常
四、示例
(1)定義一個Student類
package cn.com.infosky.Jaxb; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="Root") public class Student implements Serializable { /** * */ private static final long serialVersionUID = -8317239764136972094L; private String name; private String country; private String birthDate; /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the country */ public String getCountry() { return country; } /** * @param country * the country to set */ public void setCountry(String country) { this.country = country; } /** * @return the birthDate */ public String getBirthDate() { return birthDate; } /** * @param birthDate the birthDate to set */ public void setBirthDate(String birthDate) { this.birthDate = birthDate; } }
(2)通過Marshaller接口將Student對象序列化到TestJaxb.xml文件
public void Obj2Xml() { File xmlFile = new File("C:/TestJaxb.xml"); JAXBContext ctx; try { ctx = JAXBContext.newInstance(Student.class); Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 格式化輸出 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 設置輸出編碼,默認為UTF-8 Student stu = new Student(); stu.setName("Zhangsan"); stu.setCountry("CN"); //指定時間格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); stu.setBirthDate(sdf.format(new Date())); marshaller.marshal(stu, xmlFile); System.out.println("Obj2Xml Over!"); } catch (JAXBException e) { System.out.println("error"); System.out.println(e.toString()); System.out.println(e.getStackTrace()); // TODO: handle exception } }
運行后生成的TestJaxb.xml結構:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Root xmlns="http://www.example.org/wzhang"> <birthDate>2014-04-10</birthDate> <country>CN</country> <name>Zhangsan</name> </Root>
(3)通過Unmarshaller接口從XML文件中獲取Student對象
public void XmlToObj() { try { File file = new File("C:\\TestJaxb.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Student stu = (Student) unmarshaller.unmarshal(file); System.out.println(stu.getName()+"..."+stu.getBirthDate()); } catch (JAXBException e) { e.printStackTrace(); } }
(4)測試代碼
public static void main(String[] args) { new App().Obj2Xml(); //new App().XmlToObj(); }
示例源代碼下載,猛戳JaxbDemo.7z