JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.
Current version of JAXB is JABX 2.0
JAXB Marshalling Example(Converting Object into XML):
There are some steps to convert java object into XML document which are given below.
>Create POJO and generate the classes
>Create the JAXBContext object
>Create the Marshaller objects
>Create the content tree by using set methods
>Call the marshal method
Save as Employee.java
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
private int rollno;
private String name;
private int age;
private String college;
public Student() { }
public Student(int rollno, String name, int age,String college) {
super();
this.rollno = rollno;
this.name = name;
this.age = age;
this.college=college;
}
@XmlAttribute
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
@XmlElement
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
}
@XmlRootElement: specifies the root element for the xml document.
@XmlAttribute: specifies the attribute for the root element.
@XmlElement: specifies the sub element for the root element.
Save as ObjectToXml.java
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class ObjectToXml {
public static void main(String []args) throws Exception{
JAXBContext contextObj = JAXBContext.newInstance(Student.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Employee emp1=new Employee(15,"Vishwa Kumar",26,"KEC");
marshallerObj.marshal(emp1, new FileOutputStream("student.xml"));
}
}
Output:
The generated xml file will look like this:
student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<student rollno="15">
<name>Vishwa Kumar</name>
<age>26</age>
<college>KEC</college>
</student>
Current version of JAXB is JABX 2.0
JAXB Marshalling Example(Converting Object into XML):
There are some steps to convert java object into XML document which are given below.
>Create POJO and generate the classes
>Create the JAXBContext object
>Create the Marshaller objects
>Create the content tree by using set methods
>Call the marshal method
Save as Employee.java
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
private int rollno;
private String name;
private int age;
private String college;
public Student() { }
public Student(int rollno, String name, int age,String college) {
super();
this.rollno = rollno;
this.name = name;
this.age = age;
this.college=college;
}
@XmlAttribute
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
@XmlElement
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
}
@XmlRootElement: specifies the root element for the xml document.
@XmlAttribute: specifies the attribute for the root element.
@XmlElement: specifies the sub element for the root element.
Save as ObjectToXml.java
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class ObjectToXml {
public static void main(String []args) throws Exception{
JAXBContext contextObj = JAXBContext.newInstance(Student.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Employee emp1=new Employee(15,"Vishwa Kumar",26,"KEC");
marshallerObj.marshal(emp1, new FileOutputStream("student.xml"));
}
}
Output:
The generated xml file will look like this:
student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<student rollno="15">
<name>Vishwa Kumar</name>
<age>26</age>
<college>KEC</college>
</student>
No comments:
Post a Comment