本文主要是介绍使用 JDK(JAXB) XML 和 java对象相互转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JAXB GitHub: https://github.com/javaee/jaxb-v2
JAXB Users Guide : https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
JDK中JAXB相关的重要Class和Interface:
JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。
Marshaller接口,将Java对象序列化为XML数据。
Unmarshaller接口,将XML数据反序列化为Java对象。
JDK中JAXB相关的重要Annotation:
- @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属性。
注意:
- 对于要序列化(marshal)为XML的Java类,绝不能把成员变量声明为public,否则运行将抛出异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException。
- 对于JAXB相关的重要Annotation的声明,如@Xml…..,可以放在成员变量的setter()或getter()方法上,两者中任选其一即可,但决不能放在成员变量上,否则运行将抛出异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException。
环境要求:JDK1.6+
实例一:
package jaxb;/*** Created by sheting on 10/17/2017*/
public class Classroom {private int id;private String name;private int grade;public Classroom() {}public Classroom(int id, String name, int grade) {super();this.id = id;this.name = name;this.grade = 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;}
}
package jaxb;import javax.xml.bind.annotation.XmlRootElement;/*** Created by sheting on 10/17/2017*/
@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();}
}
package jaxb;import org.junit.Test;import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;/*** Created by sheting on 10/17/2017*/
public class TestJaxb {@Testpublic 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();}}@Testpublic 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();}}
}
注意:
1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象则不需要
2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。
实例二:
package com.mkyong.core;import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement
public class Customer {String name;int age;int id;public String getName() {return name;}@XmlElementpublic void setName(String name) {this.name = name;}public int getAge() {return age;}@XmlElementpublic void setAge(int age) {this.age = age;}public int getId() {return id;}@XmlAttributepublic void setId(int id) {this.id = id;}}
Convert Object to XML
package com.mkyong.core;import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;public class JAXBExample {public static void main(String[] args) {Customer customer = new Customer();customer.setId(100);customer.setName("mkyong");customer.setAge(29);try {File file = new File("C:\\file.xml");JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);Marshaller jaxbMarshaller = jaxbContext.createMarshaller();// output pretty printedjaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);jaxbMarshaller.marshal(customer, file);jaxbMarshaller.marshal(customer, System.out);} catch (JAXBException e) {e.printStackTrace();}}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100"><age>29</age><name>mkyong</name>
</customer>
Convert XML to Object
package com.mkyong.core;import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;public class JAXBExample {public static void main(String[] args) {try {File file = new File("C:\\file.xml");JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);System.out.println(customer);} catch (JAXBException e) {e.printStackTrace();}}
}
Output
Customer [name=mkyong, age=29, id=100]
维基百科:https://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding
这篇关于使用 JDK(JAXB) XML 和 java对象相互转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!