JAXB is an acronym derived from Java Architecture for XML Binding. This articles shows how to convert Java object to XML using JAXB annotation and vice versa. If you would like to know more about JAXB, see below links.
http://jaxb.java.net/
http://jaxb.java.net/tutorial/
http://www.oracle.com/ technetwork/articles/javase/ index-140168.html
http://jaxb.java.net/tutorial/
http://www.oracle.com/
Marshalling - Convesrting a Java object to XML is known as Marshalling.
Unmarshalling - Converting a XML to Java Object is known as Unmarshalling.
Unmarshalling - Converting a XML to Java Object is known as Unmarshalling.
About the example :
We have a Employee class which contains Name class reference in it. i.e HAS-A relation. Along with emplId and dept. TestMarshalling.java contains marshalIt() and unmarshalIt() method. marshalIt() as the name suggest, it accepts Employee object and return XML string out of it. And similarly unmarshalIt() method takes Class name and XML string and return Employee object out of it.
marshalIt(Object objectName) method uses JAXBContext to create Marshaller object
JAXBContext jaxbContext = JAXBContext.newInstance( objectName.getClass());
Marshaller marshaller = jaxbContext.createMarshaller() ;
Marshaller marshaller = jaxbContext.createMarshaller()
Similarly unmarshalIt(Class className,String xml) method use JAXBContext to create Unmarshaller object
JAXBContext jaxbContext = JAXBContext.newInstance( className);
Unmarshaller unmarshaller = jaxbContext. createUnmarshaller();
Unmarshaller unmarshaller = jaxbContext.
No need to include any JAR file if you are using Java 1.6 and above because JAXB is bundled in JDK 1.6. Directly you can run below example and see the output. However if you are using Java 1.5 or lower version you need to include JAR file (e.g JAXB2_20120218.jar ) from http://jaxb.java.net/
Below are the self explanatory Java example. Directly you can run the example and see the output no need to have any IDE (e.g. Eclipse)
Name.java
/* Name.java */
public class Name {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Employee.java
/* Employee.java */
import javax.xml.bind.annotation. XmlAttribute;
import javax.xml.bind.annotation. XmlElement;
import javax.xml.bind.annotation. XmlRootElement;
import javax.xml.bind.annotation.
import javax.xml.bind.annotation.
@XmlRootElement
public class Employee {
private String emplId;
private Name name;
private String dept;
public String getEmplId() {
return emplId;
}
//This field is used as attribute
@XmlAttribute
public void setEmplId(String emplId) {
this.emplId = emplId;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
//This field is element with custom name
@XmlElement (name="Department")
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
public class Employee {
private String emplId;
private Name name;
private String dept;
public String getEmplId() {
return emplId;
}
//This field is used as attribute
@XmlAttribute
public void setEmplId(String emplId) {
this.emplId = emplId;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
//This field is element with custom name
@XmlElement (name="Department")
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
TestMarshalling.java
/* TestMarshalling.java */
import java.io.StringReader;
import java.io.StringWriter;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class TestMarshalling {
public static void main(String[] args) throws JAXBException {
//create employee object
Employee emp = new Employee();
//create name object
Name name = new Name();
name.setFirstName("Madan");
name.setLastName("Chaudhary" );
//set name object to employee object
emp.setName(name);
emp.setEmplId("001");
emp.setDept("Development");
//get XML out of Object by Marshalling
String xml = marshalIt(emp);
System.out.println("--- Generated XML ---");
System.out.println(xml);
//get Object out of XML by Unmarshalling
Employee newEmp = (Employee) unmarshalIt(Employee.class, xml);
System.out.println("--- Generated Employee Object ---");
System.out.println(newEmp);
System.out.println(newEmp. getEmplId());
System.out.println(newEmp. getName().getFirstName());
System.out.println(newEmp. getName().getLastName());
System.out.println(newEmp. getDept());
}
public static String marshalIt(Object objectName) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance( objectName.getClass());
Marshaller marshaller = jaxbContext.createMarshaller() ;
//For Pretty printing output
marshaller.setProperty( Marshaller.JAXB_FORMATTED_ OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal( objectName, writer);
return writer.toString();
}
public static Object unmarshalIt(Class<?> className,String xml) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance( className);
Unmarshaller unmarshaller = jaxbContext. createUnmarshaller();
StringReader reader = new StringReader(xml);
return unmarshaller.unmarshal(reader) ;
}
//create employee object
Employee emp = new Employee();
//create name object
Name name = new Name();
name.setFirstName("Madan");
name.setLastName("Chaudhary"
//set name object to employee object
emp.setName(name);
emp.setEmplId("001");
emp.setDept("Development");
//get XML out of Object by Marshalling
String xml = marshalIt(emp);
System.out.println("--- Generated XML ---");
System.out.println(xml);
//get Object out of XML by Unmarshalling
Employee newEmp = (Employee) unmarshalIt(Employee.class,
System.out.println("--- Generated Employee Object ---");
System.out.println(newEmp);
System.out.println(newEmp.
System.out.println(newEmp.
System.out.println(newEmp.
System.out.println(newEmp.
}
public static String marshalIt(Object objectName) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(
Marshaller marshaller = jaxbContext.createMarshaller()
//For Pretty printing output
marshaller.setProperty(
StringWriter writer = new StringWriter();
marshaller.marshal(
return writer.toString();
}
public static Object unmarshalIt(Class<?> className,String xml) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(
Unmarshaller unmarshaller = jaxbContext.
StringReader reader = new StringReader(xml);
return unmarshaller.unmarshal(reader)
}
}
Output
$javac Name.java
$javac Employee.java
$javac TestMarshalling.java
$java TestMarshalling
--- Generated XML ---
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>--- Generated XML ---
<employee emplId="001">
<Department>Development</Department>
<name>
<firstName>Madan</firstName>
<lastName>Chaudhary</lastName>
</name>
</employee>
--- Generated Employee Object ---
Employee@d0a5d9
001
Madan
Chaudhary
Development
Employee@d0a5d9
001
Madan
Chaudhary
Development