✓ Definition
✓ Convert Java Object To XML (Marshalling Example) package com.jst.jaxb;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private Department department;
public Student() {
super();
}
public Student(Integer id, String firstName, String lastName, Department department) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "Student [ID = " + id + ", First Name = " + firstName + ", Last Name = " + lastName + ", Department [ID = " + department.getId() + ", First Name = " + department.getFirstName() +"]" + "]";
}
}
package com.jst.jaxb;
public class Department {
private Integer id;
private String firstName;
public Department() {
super();
}
public Department(Integer id, String firstName) {
super();
this.id = id;
this.firstName = firstName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
package com.jst.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ObjectToXml {
public static void main(String[] args) {
Student student = new Student(1001, "Naga Vijay", "Jaladhi", new Department(1001, "CSE"));
convertObjectToXml(student);
}
private static void convertObjectToXml(Student student) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
jaxbMarshaller.marshal(student, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Output :
CSE
1001
Naga Vijay
1001
Jaladhi
✓ Convert XML To Java Object (Unmarshaller Example)
package com.jst.jaxb;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private Department department;
public Student() {
super();
}
public Student(Integer id, String firstName, String lastName, Department department) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "Student [ID = " + id + ", First Name = " + firstName + ", Last Name = " + lastName + ", Department [ID = " + department.getId() + ", First Name = " + department.getFirstName() +"]" + "]";
}
}
package com.jst.jaxb;
public class Department {
private Integer id;
private String firstName;
public Department() {
super();
}
public Department(Integer id, String firstName) {
super();
this.id = id;
this.firstName = firstName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
package com.jst.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XmlToObject {
public static void main(String[] args) {
String fileName = "C:\\Intel\\student.xml";
convertXmlFileToObject(fileName);
}
private static void convertXmlFileToObject(String fileName) {
File xmlFile = new File(fileName);
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(Student.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Student student = (Student) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(student);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Input :
CSE
1001
Naga Vijay
1001
Jaladhi
Output : Student [ID = 1001, First Name = Naga Vijay, Last Name = Jaladhi, Department [ID = 1001, First Name = CSE]]
✓ Convert XML To Java Object (Unmarshaller Example) package com.jst.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Person")
@XmlType(propOrder = {"id", "name", "age", "gender"})
public class Person {
private int id;
private int age;
private String name;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [ID =" + id + ", Age = " + age + ", Name = " + name + ", Gender = " + gender + "]";
}
}
package com.jst.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 JaxbExample {
private static final String FILE_NAME = "C:\\Intel\\person.xml";
public static void main(String[] args) {
Person person = new Person();
person.setId(2001);
person.setName("David Ping");
person.setAge(40);
person.setGender("Male");
convertObjectToXML(person);
Person personFromFile = convertXMLToObject();
System.out.println(personFromFile.toString());
}
private static void convertObjectToXML(Person person) {
try {
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(person, new File(FILE_NAME));
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static Person convertXMLToObject() {
try {
JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Person person = (Person) unmarshaller.unmarshal(new File(FILE_NAME));
return person;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
}
Marshaller Output :
2001
David Ping
40
Male
Unmarshaller Input :
2001
David Ping
40
Male
Unmarshaller Output : Person [ID =2001, Age = 40, Name = David Ping, Gender = Male]
JAXB stands for Java Architecture for XML Binding. JAXB is an API and provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, JAXB used to convert Java Object into XML and XML into Java Object.
✓ JAXB AnnotationsAnnotation | Scope | Description |
---|---|---|
@XmlRootElement | Class and Enum | Defines the XML root element. Root Java classes need to be registered with the JAXB context when it is created. |
@XmlAccessorType | Package and Class | Defines the fields and properties of your Java classes that the JAXB engine uses for binding. It has four values: PUBLIC_MEMBER, FIELD, PROPERTY and NONE. |
@XmlAccessorOrder | Package and Class | Defines the sequential order of the children. |
@XmlType | Class and Enum | Maps a Java class to a schema type. It defines the type name and order of its children. |
@XmlElement | Field | Maps a field or property to an XML element |
@XmlAttribute | Field | Maps a field or property to an XML attribute |
@XmlTransient | Field | Prevents mapping a field or property to the XML Schema |
@XmlValue | Field | Maps a field or property to the text value on an XML tag. |
@XmlList | Field and Parameter | Maps a collection to a list of values separated by space. |
@XmlElementWrapper | Field | Maps a Java collection to an XML wrapped collection |
✓ Convert Java Object To XML (Marshalling Example) package com.jst.jaxb;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private Department department;
public Student() {
super();
}
public Student(Integer id, String firstName, String lastName, Department department) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "Student [ID = " + id + ", First Name = " + firstName + ", Last Name = " + lastName + ", Department [ID = " + department.getId() + ", First Name = " + department.getFirstName() +"]" + "]";
}
}
package com.jst.jaxb;
public class Department {
private Integer id;
private String firstName;
public Department() {
super();
}
public Department(Integer id, String firstName) {
super();
this.id = id;
this.firstName = firstName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
package com.jst.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ObjectToXml {
public static void main(String[] args) {
Student student = new Student(1001, "Naga Vijay", "Jaladhi", new Department(1001, "CSE"));
convertObjectToXml(student);
}
private static void convertObjectToXml(Student student) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
jaxbMarshaller.marshal(student, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Output :
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private Department department;
public Student() {
super();
}
public Student(Integer id, String firstName, String lastName, Department department) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "Student [ID = " + id + ", First Name = " + firstName + ", Last Name = " + lastName + ", Department [ID = " + department.getId() + ", First Name = " + department.getFirstName() +"]" + "]";
}
}
package com.jst.jaxb;
public class Department {
private Integer id;
private String firstName;
public Department() {
super();
}
public Department(Integer id, String firstName) {
super();
this.id = id;
this.firstName = firstName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
package com.jst.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XmlToObject {
public static void main(String[] args) {
String fileName = "C:\\Intel\\student.xml";
convertXmlFileToObject(fileName);
}
private static void convertXmlFileToObject(String fileName) {
File xmlFile = new File(fileName);
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(Student.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Student student = (Student) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(student);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Input :
✓ Convert XML To Java Object (Unmarshaller Example) package com.jst.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Person")
@XmlType(propOrder = {"id", "name", "age", "gender"})
public class Person {
private int id;
private int age;
private String name;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [ID =" + id + ", Age = " + age + ", Name = " + name + ", Gender = " + gender + "]";
}
}
package com.jst.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 JaxbExample {
private static final String FILE_NAME = "C:\\Intel\\person.xml";
public static void main(String[] args) {
Person person = new Person();
person.setId(2001);
person.setName("David Ping");
person.setAge(40);
person.setGender("Male");
convertObjectToXML(person);
Person personFromFile = convertXMLToObject();
System.out.println(personFromFile.toString());
}
private static void convertObjectToXML(Person person) {
try {
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(person, new File(FILE_NAME));
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static Person convertXMLToObject() {
try {
JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Person person = (Person) unmarshaller.unmarshal(new File(FILE_NAME));
return person;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
}
Marshaller Output :