Entities Class
Create new package named entities. In this package, create new java classes as below:
Student Entity
package entities;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
public class Student implements Serializable {
private String id;
private String name;
private int age;
private double score;
private Address address;
public String getId() {
return id;
}
@XmlElement(name = "id")
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement(name = "name")
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement(name = "age")
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
@XmlElement(name = "score")
public void setScore(double score) {
this.score = score;
}
@XmlElement(name = "address")
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
/* Declare address tag */
@XmlRootElement(name = "address")
public static class Address implements Serializable {
private String street;
private String ward;
private String district;
private String country;
public String getStreet() {
return street;
}
@XmlElement
public void setStreet(String street) {
this.street = street;
}
public String getWard() {
return ward;
}
@XmlElement
public void setWard(String ward) {
this.ward = ward;
}
public String getDistrict() {
return district;
}
@XmlElement
public void setDistrict(String district) {
this.district = district;
}
public String getCountry() {
return country;
}
@XmlElement
public void setCountry(String country) {
this.country = country;
}
public Address() {
}
public Address(String street, String ward, String district, String country) {
this.street = street;
this.ward = ward;
this.district = district;
this.country = country;
}
}
}
Run Application
Create new package, named demo. In this package, create new java class, named Main as below
package demo;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import entities.Student;
public class Main {
private static void serialize(Student student, String xmlFile) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Display result to console
marshaller.marshal(student, System.out);
// Save result to xml file
marshaller.marshal(student, new File(xmlFile));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
private static void deSerialize(String xmlFile) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Student student = (Student) unmarshaller.unmarshal(new File(xmlFile));
System.out.println("Student Info");
System.out.println("Id: " + student.getId());
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("Score: " + student.getScore());
System.out.println("Address Information");
System.out.println("\tStreet: " + student.getAddress().getStreet());
System.out.println("\tWard: " + student.getAddress().getWard());
System.out.println("\tDistrict: " + student.getAddress().getDistrict());
System.out.println("\tCountry: " + student.getAddress().getCountry());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public static void main(String[] args) {
String xmlFile = "src\\data\\student.xml";
Student student = new Student();
student.setId("st01");
student.setName("name 1");
student.setAge(22);
student.setScore(7.8);
student.setAddress(new Student.Address("street 1", "ward 1", "district 1", "country 1"));
serialize(student, xmlFile);
deSerialize(xmlFile);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<address>
<country>country 1</country>
<district>district 1</district>
<street>street 1</street>
<ward>ward 1</ward>
</address>
<age>22</age>
<id>st01</id>
<name>name 1</name>
<score>7.8</score>
</student>
Student Info
Id: st01
Name: name 1
Age: 22
Score: 7.8
Address Information
Street: street 1
Ward: ward 1
District: district 1
Country: country 1