JAXB stands for Java Architecture for XML Binding. It is a library that uses annotations to convert Java objects to XML content and vice versa. In this tutorial, I will guide you all how to convert XML file content to Java object using JAXB.
I’ll start with a project like this:
We have a simple XML file with the following contents:
1 2 3 4 5 6 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <student> <name>John</name> <code>12345</code> <age>19</age> </student> |
And a Student class will be defined using the JAXB annotation as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.huongdanjava.jaxb; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Student { private String name; private String age; private String code; public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public String getAge() { return age; } @XmlElement public void setAge(String age) { this.age = age; } public String getCode() { return code; } @XmlElement public void setCode(String code) { this.code = code; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", code=" + code + "]"; } } |
With JAXB, we usually use two basic annotations, which are:
- @XmlRootElement: This annotation specifies what the outermost tag of an XML file is and thus it is declared at the beginning of a class.
- @XmlElement: Used to declare a property of an object as a tag of an XML file.
In the above example, inside the <student> tag, we have three tags: <name>, <age> and <code>. Therefore, our Student object must declare the annotation for the three corresponding properties.
OK, now we will proceed to convert this XML content.
First, we will initialize the JAXBContext object with the object to be converted to, Student.
1 |
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); |
In this JAXBContext object, it has a method to create an object that converts XML content to a Java object: Unmarshaller. As follows:
1 |
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); |
And now you can use the unmarshal() method in the Unmarshaller object for our purposes.
There are many methods of overloaded unmarshal() but in this tutorial we are working on the file so we will create the File object and pass it to the unmarshal() method.
1 2 3 |
File file = new File("student.xml"); Student student = (Student) jaxbUnmarshaller.unmarshal(file); System.out.println(student); |
Result:
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.huongdanjava.jaxb; 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 { JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); File file = new File("student.xml"); Student student = (Student) jaxbUnmarshaller.unmarshal(file); System.out.println(student); } catch (JAXBException e) { e.printStackTrace(); } } } |