JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
mavn仓库的地址:http://mvnrepository.com/
在mavn中引入JAXB的包:
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.12</version> </dependency>
首先建一个实体类:
package com.zhcw.practice; import sun.management.Agent; import javax.xml.bind.annotation.*; /** * Created by Administrator on 2016/12/13. */ @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name="Customer") @XmlType(name="Customer",propOrder = {"name", "age"}) public class Customer { @XmlAttribute private int id; @XmlElement private String name; @XmlElement private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Customer{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
然后建立测试类:
package com.zhcw.practice; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import java.io.File; /** * Created by Administrator on 2016/12/13. */ public class JaxbExample { public static void main(String[] args) { Customer customer = new Customer(); customer.setId(1); customer.setAge(23); customer.setName("Hello word"); //将javaBean转为Xml javaBeanForXml(customer); //将Xml转为javaBean File file = new File("F:\\testXMl.xml"); XmlForJavaBean(file); } private static void javaBeanForXml(Customer customer) { try { File file = new File("F:\\testXMl.xml"); //初始化JAXBContext.JAXBContext类提供的JAXB API的客户端的入口点。 //它提供一个抽象的用于管理XML / Java绑定的必要信息,以实现JAXB绑定框架行动:解组,编组和验证。 JAXBContext jc = JAXBContext.newInstance(Customer.class); //将Java对象Marshal成XML内容的Marshal的初始化设置. Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(customer, file); marshaller.marshal(customer, System.out); } catch (JAXBException e) { System.out.println("output xml error"); e.printStackTrace(); } } private static void XmlForJavaBean(File file) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Customer customer = (Customer) unmarshaller.unmarshal(file); System.out.println(customer.toString()); } catch (JAXBException e) { System.out.println("xml for javaBean Error"); e.printStackTrace(); } } }控制台输出:
Connected to the target VM, address: '127.0.0.1:63217', transport: 'socket'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer id="1">
<name>Hello word</name>
<age>23</age>
</Customer>
Customer{id=1, name='Hello word', age=23}
Disconnected from the target VM, address: '127.0.0.1:63217', transport: 'socket'
Process finished with exit code 0