Monday, February 16, 2015

JAXB sample

JAXB, stands for Java Architecture for XML Binding, using JAXB annotation to convert Java object to / from XML file.

No need of external jar library files. JAXB is bundled in from JDK 1.6.

Basic features :
Marshalling – Convert a Java object into a XML file.
Unmarshalling – Convert XML content into a Java Object.

Model class
create a model class by using jaxb annotation to convert model class into xml file.

Generate/Convert Model object to xml
JAXB marshalling convert customer object into a XML file i.e. jaxbMarshaller.marshal().

Convert XML to Object
JAXB unmarshalling convert a XML file content into a customer object i.e. jaxbMarshaller.unmarshal()

Full source code:
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Rivet Systems
*/
@XmlRootElement
public class Customer {
String name;
String email;
int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
@XmlElement
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
}
view raw Customer hosted with ❤ by GitHub
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
/**
*
* @author Rivet Systems
*/
public class JAXBMarshall {
public static void main(String[] args) {
Customer customer = new Customer();
customer.setId(100);
customer.setName("uttesh");
customer.setEmail("uttesh@gmail.com");
try {
File file = new File("customer.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
view raw JAXBMarshall hosted with ❤ by GitHub
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
*
* @author Rivet Systems
*/
public class JAXBUnMarshall {
public static void main(String[] args) {
try {
File file = new File("customer.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println("Customer Name :"+customer.getName());
System.out.println("Customer Email :"+customer.getEmail());
System.out.println("Customer Id :"+customer.getId());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
view raw JAXBUnMarshall hosted with ❤ by GitHub

Related Posts:

  • JAXB sampleJAXB, stands for Java Architecture for XML Binding, using JAXB annotation to convert Java object to / from XML file. No need of external jar library files. JAXB is bundled in from JDK 1.6. Basic features : Marshalling – C… Read More
  • Read multiple xml nodes by JAXB / converting xml to objectUsing JAXB we can read/write the multiple xml nodes. consider a scenario where u have to read the list of items from the file system, then its better option is to use JSON or XML structure. Full sample of the JAXB reading t… Read More

0 comments:

Post a Comment