Monday, 27 January 2014

JAXB Hello World example

JAXB stands for Java Architecture for XML Binding. This helps to convert Java objects to XML and XML objects to java. JAXB provides marshalling and unmarshalling to do this.
Marshalling - Convert Java object to XML format
Unmarshalling -Convert XML to Java Object

Marshalling and unmarshalling can be done with JAXB annotated java beans. So first let’s create a bean with a name Customer.

Customer.Java
package jaxb;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customer {
private String FisrtName;
private String LastName;
private Date dob;
private String emailAddress;
public String getFisrtName() {
return FisrtName;
}
public void setFisrtName(String fisrtName) {
FisrtName = fisrtName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}

Marshalling:
Here we will create the customer object and generate the xml file using the JAXB.
Marshalling.java
package jaxb;
import java.io.File;
import java.util.Date;import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Marshalling {
public static void main(String args[])throws JAXBException{
/**
* Create Customer Object
*/
Customer customer = new Customer();
customer.setFisrtName("Sivaranjani");
customer.setLastName("Rajadurai");
customer.setEmailAddress("siva@gmail.com");
customer.setDob(new Date());

/**
* Creating Jaxb Context and Marshaller
*/
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

/**
* Creates the xml output in the console
*/
marshaller.marshal(customer, System.out);

/**
* Create xml output in a file
*/
File file = new File("JaxbXml.xml");
marshaller.marshal(customer, file);
}
}

Output:
First marshaller.marshal(customer, System.out); function prints the output on the console as below.
<customer>
<dob>2014-01-27T23:06:00.547+05:30</dob>
<emailAddress>siva@gmail.com</emailAddress>
<fisrtName>Sivaranjani</fisrtName>
<lastName>Rajadurai</lastName>
</customer>

Second function marshaller.marshal(customer, file); creates a file named JaxbXml.xml at class path.

UnMarshalling.java
package jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class UnMarshalling {
public static void main(String args[]) throws JAXBException {
/**
* Creating Jaxb Context and Marshaller
*/
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();

/**
* Get the xml file to convert
*/
File file = new File("JaxbXml.xml");

/**
* Unmarshaling the file
*/
Customer customer = (Customer) unMarshaller.unmarshal(file);
System.out.println("First Name: " + customer.getFisrtName() + "\n"
+ "Last Name: " + customer.getLastName());
}
}

Output:
First Name: Sivaranjani
Last Name: Rajadurai

1 comment: