下面使用的是JDK自带的类,没有引用任何第三方jar包。
Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 Java 内容对象树。
备注:marshal(序列化、排列、整理)
Marshaller 类使客户端应用程序能够将 Java 内容树转换回 XML 数据。
Java代码
![收藏代码](https://i-blog.csdnimg.cn/blog_migrate/80474d00c8843888f1d230a55dc71f00.png)
- package hb.jaxb;
- public class Classroom {
- private int id;
- private String name;
- private int grade;
- 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 getGrade() {
- return grade;
- }
- public void setGrade(int grade) {
- this.grade = grade;
- }
- public Classroom(int id, String name, int grade) {
- super();
- this.id = id;
- this.name = name;
- this.grade = grade;
- }
- public Classroom() {
- super();
- }
- }
Java代码
![收藏代码](https://i-blog.csdnimg.cn/blog_migrate/80474d00c8843888f1d230a55dc71f00.png)
- package hb.jaxb;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- public class Student {
- private int id;
- private String name;
- private int age;
- private Classroom classroom;
- 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;
- }
- public Classroom getClassroom() {
- return classroom;
- }
- public void setClassroom(Classroom classroom) {
- this.classroom = classroom;
- }
- public Student(int id, String name, int age, Classroom classroom) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- this.classroom = classroom;
- }
- //无参够着函数一定需要,否则JXBContext无法正常解析。
- public Student() {
- super();
- }
- }
注意:
1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象则不需要
2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。
Java代码
![收藏代码](https://i-blog.csdnimg.cn/blog_migrate/80474d00c8843888f1d230a55dc71f00.png)
- package hb.jaxb;
- import java.io.StringReader;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- import javax.xml.bind.Unmarshaller;
- import org.junit.Test;
- public class TestJaxb {
- @Test
- public void beanToXML() {
- Classroom classroom = new Classroom(1, "软件工程", 4);
- Student student = new Student(101, "张三", 22, classroom);
- try {
- JAXBContext context = JAXBContext.newInstance(Student.class);
- Marshaller marshaller = context.createMarshaller();
- marshaller.marshal(student, System.out);
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }
- @Test
- public void XMLStringToBean(){
- String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>22</age><classroom><grade>4</grade><id>1</id><name>软件工程</name></classroom><id>101</id><name>张三</name></student>";
- try {
- JAXBContext context = JAXBContext.newInstance(Student.class);
- Unmarshaller unmarshaller = context.createUnmarshaller();
- Student student = (Student)unmarshaller.unmarshal(new StringReader(xmlStr));
- System.out.println(student.getAge());
- System.out.println(student.getClassroom().getName());
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }
- }
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
JAXBContext 类提供到 JAXB API 的客户端入口点。它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。