java和XML轉換(一)
阿新 • • 發佈:2019-01-29
用的是jdk自帶的javax.xml.bind.JAXBContext將物件和xml字串進行相互轉換。
比較常用的幾個:
@XmlRootElement:根節點
@XmlAttribute:該屬性作為xml的attribute
@XmlElement:該屬性作為xml的element,且可以增加屬性(name=”NewElementName”),那麼生成的xml串的elment的標籤是NewElementName
首先,java註解讀取XML。
一、新建school.xml檔案,內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<SCHOOL>
<name>延邊大學</name>
<address>吉林延吉市</address>
<student grade="1">
<num>001</num>
<name>小杜</name>
<age>23</age>
</student>
<student grade="2">
<num>002</num>
<name>小張</name>
<age>21</age>
</student>
</SCHOOL>
二、根據配置檔案書寫對應的註解類。
編寫Student基類,內容如下:
package com.cvicse.ump.xml.school;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class Student {
private String studentNum;
private String studentName;
private String studentGrade;
private int age;
@XmlElement(name="num")
public String getStudentNum() {
return studentNum;
}
public void setStudentNum(String studentNum) {
this.studentNum = studentNum;
}
@XmlElement(name="name")
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@XmlAttribute(name="grade")
public String getStudentGrade() {
return studentGrade;
}
public void setStudentGrade(String studentGrade) {
this.studentGrade = studentGrade;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [studentNum=" + studentNum + ", studentName="
+ studentName + ", studentGrade=" + studentGrade + ", age="
+ age + "]";
}
}
編寫基類school,內容如下:
package com.cvicse.ump.xml.school;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="SCHOOL")
public class School {
private String name;
private String address;
private List<Student>students;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@XmlElement(name="student")
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "School [name=" + name + ", address=" + address + "]";
}
}
編寫XML讀取程式:
package com.cvicse.ump.xml.school;
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class SchoolRead {
public static void main(String[] args) {
File file = new File("src/school.xml");
try {
JAXBContext context = JAXBContext.newInstance(School.class);
Unmarshaller unMar = context.createUnmarshaller();
School school = (School) unMar.unmarshal(file);
System.out.println(school);
List<Student>students = school.getStudents();
for(Student student:students){
System.out.println(student);
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
編寫寫入檔案程,內容如下
package com.cvicse.ump.xml.school;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class SchoolWrite {
public static void main(String[] args) {
School school = new School();
school.setName("實驗小學");
school.setAddress("山東濟南市");
List<Student> students = new ArrayList<Student>();
Student student1 = new Student();
student1.setStudentNum("001");
student1.setStudentName("xiaoDy");
student1.setAge(25);
student1.setStudentGrade("1");
Student student2 = new Student();
student2.setStudentNum("002");
student2.setStudentName("xiaoWf");
student2.setAge(22);
student2.setStudentGrade("2");
students.add(student1);
students.add(student2);
school.setStudents(students);
try {
JAXBContext context = JAXBContext.newInstance(School.class);
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
mar.marshal(school, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}