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 on how to convert content of XML file to Java objects using JAXB.
I’ll start with a Maven project like this:
The first thing you need to know about this JAXB library is that from Java 8 and earlier, the JAXB library was built-in with Java, which means you do not need to declare any JAXB dependencies to work with it. However, from Java 9 onwards, JAXB has been separated into an independent module.
If you are using Java EE 8, you can declare the following dependencies to work with JAXB:
1 2 3 4 5 |
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> |
If you use the Jakarta EE namespace for all versions from Java 9 onwards, declare the following dependency:
1 2 3 4 5 |
<dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.2</version> </dependency> |
My project uses Java 21 so I will declare the namespace Jakarta EE with the implementation being the jaxb-impl library as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
<dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>4.0.5</version> <scope>runtime</scope> </dependency> |
To give an example for this tutorial, I 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 jakarta.xml.bind.annotation.XmlElement; import jakarta.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 |
package com.huongdanjava.jaxb; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Unmarshaller; import java.io.File; 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(); } } } |