Convert a Java Object to XML using JAXB
In the following tutorial we will show you how you can convert a Java Object to XML.
Annotations Explained
First we need to define our model that will be converted to XML representation. Here are a couple of annotations with their explanations what they do.
- @XmlRootElement: This annotation defines the root element. It is required by JAX-B, without this, the object cannot be converted into xml.
- @XmlAccessorType: This tells JAX-B witch access level is used.
- NONE: Indicates that JAX-B will not convert any properties.
- FIELD: Indicates that JAX-B will search for annotations on field level.
- PROPERTY: Indicates that JAX-B will search for annotations on property level.
- PUBLIC_MEMBER (default): Indicates that JAX-B will search for public members.
- @XmlAttribute: This tels JAX-B to convert the property or field into an XML attribute.
- @XmlElement: This tells JAX-B to convert the property or field into an XML element. This is not required. By default JAX-B uses every public non-transient member into an element if the class is annotated with
@XmlRootElement
.
The Model
package com.memorynotfound.xml.jaxb;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "Course")
@XmlAccessorType(XmlAccessType.FIELD)
public class Course {
@XmlAttribute
private long id;
@XmlElement
private String name;
private String description;
/**
* Default no-args constructor needed by JAX-B
*/
public Course() {
}
public Course(long id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Convert Object to XML
Here is how to marshal the java object to xml.
package com.memorynotfound.xml.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ConvertObjectToXml {
public static void main(String... args) throws JAXBException {
// create the object
Course course = new Course(1, "JAX-B", "Learning Java Architecture for XML Binding(JAX-B)");
// create jaxb context
JAXBContext jaxbContext = JAXBContext.newInstance(Course.class);
// create jaxb marshaller
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// pretty print xml
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// marshal object
jaxbMarshaller.marshal(course, System.out);
}
}
Pretty Print XML
If you want you can pretty print your XML for easy visual confirmation. you can achieve this by setting the Marshaller.JAXB_FORMATTED_OUTPUT
property to true
.
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<course id="1">
<name>JAX-B</name>
<description>Learning Java Architecture for XML Binding(JAX-B)</description>
</course>