Main.java:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class Main {
private final static File FILE = getFILE() ;
private static File getFILE() {
final String PATH = String.format("%stest.xml", Main.class.getClassLoader().getResource("").getPath());
final File FILE = new File(PATH);
return FILE;
}
public static void main(String[] args) {
//write
classToXML();
//read
xmlToClass();
}
private static void classToXML() {
Transcript transcript = new Transcript()
.addStudentTolist(new Student("王曉明", new Ch(99), new En(50), new Math(100)))
.addStudentTolist(new Student("你好嗎", new Ch(69), new En(90), new Math(10)));
try {
JAXBContext context = JAXBContext.newInstance(new Class[]{Transcript.class, Ch.class, En.class, Math.class, Transcript.class});
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshaller.marshal(transcript, FILE);
System.out.println("Output test.xml done");
} catch (JAXBException e) {
e.printStackTrace();
}
}
private static void xmlToClass() {
Transcript transcript = null;
try {
JAXBContext context = JAXBContext.newInstance(new Class[]{Transcript.class, Ch.class, En.class, Math.class, Transcript.class});
Unmarshaller unmarshaller = context.createUnmarshaller();
transcript = (Transcript) unmarshaller.unmarshal(FILE);
for (Student student : transcript.getList()) {
System.out.printf("name:%s ch:%d en:%d math:%d\n",
student.getName(),
student.getCh().getScore(),
student.getEn().getScore(),
student.getMath().getScore());
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Score.java:
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "score")
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Score {
@XmlValue
private Integer score;
protected Score() {
}
protected Score(Integer score) {
this.score = score;
}
protected Score score(Integer score) {
this.score = score;
return this;
}
public void setScore(Integer score) {
this.score = score;
}
public Integer getScore() {
return score;
}
}
Ch.java:
public class Ch extends Score {
public Ch() {
super();
}
public Ch(Integer score) {
super(score);
}
public Ch score(Integer score) {
return (Ch) super.score(score);
}
}
En.java:
public class En extends Score{
public En() {
super();
}
public En(Integer score) {
super(score);
}
public En score(Integer score) {
return (En) super.score(score);
}
}
Math.java:
public class Math extends Score {
public Math() {
super();
}
public Math(Integer score) {
super(score);
}
public Math score(Integer score) {
return (Math)super.score(score);
}
}
Student.java:
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {
private String name;
private Ch ch;
private En en;
private Math math;
public Student() {
}
public Student(String name) {
this.name = name;
}
public Student(String name, Ch ch, En en, Math math) {
this.name = name;
this.ch = ch;
this.en = en;
this.math = math;
}
public Student name(String name) {
this.name = name;
return this;
}
public Student ch(Ch ch) {
this.ch = ch;
return this;
}
public Student en(En en) {
this.en = en;
return this;
}
public Student math(Math math) {
this.math = math;
return this;
}
public void setName(String name) {
this.name = name;
}
public void setCh(Ch ch) {
this.ch = ch;
}
public void setEn(En en) {
this.en = en;
}
public void setMath(Math math) {
this.math = math;
}
public String getName() {
return name;
}
public Ch getCh() {
return ch;
}
public En getEn() {
return en;
}
public Math getMath() {
return math;
}
}
Transcript.java:
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Transcript {
@XmlElement(name = "Student")
private List<Student> list = new ArrayList<Student>();
public Transcript() {
}
public Transcript(List<Student> list) {
this.list = list;
}
public Transcript addStudentTolist(Student student) {
this.list.add(student);
return this;
}
public void setList(List<Student> list) {
this.list = list;
}
public List<Student> getList() {
return list;
}
}
輸出結果:
參考資料:
https://docs.oracle.com/javase/tutorial/jaxb/intro/index.html