Searching...
Wednesday, 19 September 2012

Java XML JAXB Example

02:45

What is JAXB ?

JAXB ( Java Architecture for XML Binding) is annotation based API for convert Java Object to xml and xml to Java Objects. it is easier to access XML documents from applications written in Java.
Below are the main process in this api
1. Marshalling – Convert a Java object into a XML file.
2. Unmarshalling – Convert XML content into a Java Object
JAXB classes
javax.xml.bind.JAXBContext ->The JAXBContext class provides the client's entry point to the JAXB API.
javax.xml.bind.JAXBException->This is the root exception class for all JAXB exceptions.
javax.xml.bind.Marshaller->The Marshallerclass is responsible for governing the process of serializing Java content trees back into XML data
javax.xml.bind.Unmarshaller->The Unmarshallerclass governs the process of deserializing XML data into Java Object.

Added annotations in POJO

package mycollectiontest;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {
String name;
String designation;
String address;
String employer;
int id;

@Override
public String toString() {
return "Customer [name=" + name + ", designation=" + designation +
", address=" + address + ", employer=" + employer + ", id=" + id
+ "]";
}

public String getName() {
return name;
}

@XmlElement
public void setName(String name) {
this.name = name;
}

public String getDesignation() {
return designation;
}

@XmlElement
public void setDesignation(String designation) {
this.designation = designation;
}

public String getAddress() {
return address;
}

@XmlElement
public void setAddress(String address) {
this.address = address;
}

public String getEmployer() {
return employer;
}

@XmlElement
public void setEmployer(String employer) {
this.employer = employer;
}

public int getId() {
return id;
}

@XmlAttribute
public void setId(int id) {
this.id = id;
}

}
Main class to test object to xml and xml to object
package mycollectiontest;

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 JAXBExample {

public static void main(String[] args) {

// Object to xml

Customer customer = new Customer();
customer.setName("vinod");
customer.setAddress("Bangalore");
customer.setDesignation("Publisher");
customer.setEmployer("Self employment");
try {

File file = new File("customer.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}

try {
File file1 = new File("customer.xml");
JAXBContext jaxbContext1 = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext1.createUnmarshaller();
Customer pc = (Customer) jaxbUnmarshaller.unmarshal(file1);
System.out.println(pc.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
// XML to object

}

}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="0">
<address>Bangalore</address>
<designation>Publisher</designation>
<employer>Self employment</employer>
<name>vinod</name>
</customer>
Customer [name=vinod, designation=Publisher, address=Bangalore,
employer=Self employment, id=0]
Reference
Oracle

0 comments: