The Ultimate Guide to Using JAXB Parser for XML and Java
This article is also available on my website — https://marateek.com/jaxb/
Java Architecture for XML Binding (JAXB) is a framework provided by Java that enables developers to convert XML documents to and from Java objects. This conversion process is known as unmarshalling (XML to Java) and marshalling (Java to XML). JAXB works by creating a mapping between the XML schema and Java classes, which enables the conversion process.
In this article, we will explore how to use JAXB parser to parse XML documents into Java objects and vice versa.
Getting Started
Before we start using JAXB, we need to ensure that it is included in our project's dependencies. JAXB is included in the Java Development Kit (JDK) since version 6, but if you are using an older version, you will need to add the JAXB dependencies manually.
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
Unmarshalling (XML to Java)
To unmarshal an XML document into a Java object, we need to follow these steps:
- Create a JAXBContext: We need to create a JAXBContext that contains the mapping between the XML schema and the Java classes. We can create it using the
JAXBContext.newInstance()
method. - Create an Unmarshaller: The Unmarshaller is responsible for converting the XML document into a Java object. We can create it using the
JAXBContext.createUnmarshaller()
method. - Unmarshal the XML document: Finally, we can use the Unmarshaller to unmarshal the XML document into a Java object using the
unmarshal()
method.
Here's an example code snippet that demonstrates how to unmarshal an XML document:
// Step 1: Create a JAXBContext
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
// Step 2: Create an Unmarshaller
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// Step 3: Unmarshal the XML document
File xmlFile = new File("employee.xml");
Employee employee = (Employee) unmarshaller.unmarshal(xmlFile);
In this example, we have an XML document that contains information about an employee. We create a JAXBContext that contains the mapping between the XML schema and the Employee
class. Then, we create an Unmarshaller and use it to unmarshal the XML document into a Java object.
Marshalling (Java to XML)
To marshal a Java object into an XML document, we need to follow these steps:
- Create a JAXBContext: We need to create a JAXBContext that contains the mapping between the XML schema and the Java classes. We can create it using the
JAXBContext.newInstance()
method. - Create a Marshaller: The Marshaller is responsible for converting the Java object into an XML document. We can create it using the
JAXBContext.createMarshaller()
method. - Marshal the Java object: Finally, we can use the Marshaller to marshal the Java object into an XML document using the
marshal()
method.
Here's an example code snippet that demonstrates how to marshal a Java object:
// Step 1: Create a JAXBContext
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
// Step 2: Create a Marshaller
Marshaller marshaller = jaxbContext.createMarshaller();
// Step 3: Marshal the Java object
Employee employee = new Employee("John", "Doe", 30);
marshaller.marshal(employee, new File("employee.xml"));
In this example, we create a Employee
object and then create a JAXBContext that contains the mapping between the XML schema and the Employee
class. We create a Marshaller and use it to marshal the Employee
object into an XML document.
Advanced Usage of JAXB parser
JAXB also provides some advanced features that can help us customize the mapping between the XML schema and the Java classes. Let's take a look at some of these features:
- @XmlRootElement: The
@XmlRootElement
annotation can be used to specify the root element of the XML document. - @XmlElement: The
@XmlElement
annotation can be used to specify the element name of a field or property in the XML document. - @XmlAttribute: The
@XmlAttribute
annotation can be used to specify that a field or property should be mapped to an XML attribute. - @XmlTransient: The
@XmlTransient
annotation can be used to specify that a field or property should be ignored during the marshalling and unmarshalling process. - @XmlAccessorType: The
@XmlAccessorType
annotation can be used to specify the access type for the fields and properties in the Java class.
Here's an example code snippet that demonstrates how to use these annotations:
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
@XmlElement(name = "first-name")
private String firstName;
@XmlElement(name = "last-name")
private String lastName;
@XmlAttribute
private int age;
@XmlTransient
private String password;
// getters and setters
}
In this example, we use annotations to specify the root element name, element names, attribute name, and access type of the fields in the Employee
class.
Conclusion
In this article, we have learned how to use JAXB to parse XML documents into Java objects and vice versa. We have seen how to unmarshal an XML document and how to marshal a Java object using a JAXBContext, Unmarshaller, and Marshaller. We have also explored some advanced features provided by JAXB, such as annotations that can be used to customize the mapping between the XML schema and the Java classes. With these tools, we can easily handle XML documents in our Java applications.
Thanks for reading!
Follow me on Twitter, I always tweet about new articles, so you won’t miss any.
Do support our publication by following it