一、重要類別說明
JAXBContext類,是應用程式的入口,用於管理XML/Java綁定資訊。
Marshaller介面,將Java對轉換為XML資料。
Unmarshaller接口,將XML轉換為Java物件。
@XmlType,將Java類別或枚舉類型對應到XML模式類型
@XmlAccessorType(XmlAccessType.FIELD) ,控製欄位或屬性的序列化。 FIELD表示JAXB將自動綁定Java類別中的每個非靜態的(static)、
非瞬態的(由@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屬性。
二、實例
1、JavaBean
import java.util.Date; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.tzz.util.xml.JaxbDateSerializer; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "country") @XmlType(propOrder = { "id", "createDate", "name", "provinceList" }) public class Country { private Integer id; @XmlJavaTypeAdapter(JaxbDateSerializer.class) private Date createDate; @XmlElement(name = "name") private String name; @XmlElementWrapper(name = "provinces") @XmlElement(name = "province") private List<Province> provinceList; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getName() { 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; } }
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = { "name", "provCity" }) public class Province { @XmlElement(name = "name") private String name; @XmlElement(name = "provCity") private String provCity; public String getProvCity() { return provCity; } public void setProvCity(String provCity) { this.provCity = provCity; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2、轉換工具類別
import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; /** * Jaxb 2.0 * */ public class JaxbXmlUtil { @SuppressWarnings("unchecked") public static <T> T xmlToBean(String xml, Class<T> beanClass) { try { JAXBContext jaxbContext = JAXBContext.newInstance(beanClass); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); T user = (T) unmarshaller.unmarshal(new StringReader(xml)); return user; } catch (JAXBException e) { e.printStackTrace(); return null; } } /**Bean转XML*/ public static String beanToXml(Object obj) { String result = null; try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); //决定是否在转换成xml时同时进行格式化(即按标签自动换行,否则即是一行的xml) marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //Marshaller.JAXB_ENCODING xml的编码方式 marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } }
3、測試類別
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.junit.Test; import com.tzz.test.util.xml.entity.Country; import com.tzz.test.util.xml.entity.Province; import com.tzz.util.xml.JaxbXmlUtil; public class TestJaxbBeanToXmlUtil { @Test public void testBeanToXml() { Country country = new Country(); country.setId(1); country.setCreateDate(new Date()); country.setName("中国"); List<Province> list = new ArrayList<Province>(); Province province = new Province(); province.setName("广东省"); province.setProvCity("广州市"); Province province2 = new Province(); province2.setName("湖南省"); province2.setProvCity("长沙市"); list.add(province); list.add(province2); country.setProvinceList(list); String xml = JaxbXmlUtil.beanToXml(country); System.out.println(xml); } @Test public void testXmlToBean() { String xml = "<?xml version='1.0' encoding='utf-8' ?>" + "<country>" + "<id>1</id>" + "<createDate>2016-03-10 09:36:01</createDate>" + "<name>中国</name>" + "<provinces>" + "<province><name>广东省</name><provCity>广州市</provCity></province>" + "<province><name>湖南省</name><provCity>长沙市</provCity></province>" + "</provinces>" + "</country>"; Country country = JaxbXmlUtil.xmlToBean(xml, Country.class); System.out.println(country.getId() + "--" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(country.getCreateDate()) + "--" + country.getName()); for (Province p : country.getProvinceList()) { System.out.println(p.getName() + "---" + p.getProvCity()); } } }
4、測試結果
4.1、執行testBeanToXml方法
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <country> <id>1</id> <createDate>2016-03-10 05:24:57</createDate> <name>中国</name> <provinces> <province> <name>广东省</name> <provCity>广州市</provCity> </province> <province> <name>湖南省</name> <provCity>长沙市</provCity> </province> </provinces> </country>
4.2、執行testXmlToBean方法
1--2016-03-10 09:36:01--中国 广东省---广州市 湖南省---长沙市
以上是Jaxb2實作Bean與xml互轉的範例程式碼詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!