常用注解
@XmlRootElement(name = “report”)
将Java类或枚举类型映射成XML中根元素,设置name属性的值可指定义根元素名称,不设置则默认为类型首字母小写的名称。
@XmlType(propOrder = {“rmsId”, “department”, “userId”, “userName”,“reason”,“time”,“conclusion”,“attachList”})
将Java类后枚举类型映射到XML模式类型,设置propOrder可以指定生成XML中各元素的先后顺序。
@XmlElement(name=“attach”)
将JavaBean中的字段值映射到XML元素,设置name属性的值可指定XML元素的名称。
@XmlAttribute(name = “”, required = false)
将JavaBean中的字段映射到XML中元素的属性,设置name属性的值可指定xml中元素属性的名称;
至于required属性,官方文档是说指定 XML 模式属性是可选的还是必需的,不是特别理解。
@XmlElementWrapper(name=“attachList”)
为集合生成xml包装器元素,简单来讲就是使XML元素更有层次感,更美观,该注解只能用在集合上。
例子
1、类结构如下
AssistReportItem 与AssistReportMsg为映射对象;
JAXBUtils提供读写XML的方法;
JAXBXMLTest提供读写测试方法;
2、各个类如下
AssistReportItem 类
package cn.hb.jaxb1;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
/**
-
@author:lwz
-
@date:2019/10/16 20:07
-
@description:
-
@modefiled by:
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = “attach”)
public class AssistReportItem {
private String subjectId;
private String subjectName;
private String objectId;
private String objectName;
private String opName;
private String opContent;
private String opResult;
private String logTime;
private String category;
private String remark;public String getSubjectId() {
return subjectId;
}public void setSubjectId(String subjectId) {
this.subjectId = subjectId;
}public String getSubjectName() {
return subjectName;
}public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}public String getObjectId() {
return objectId;
}public void setObjectId(String objectId) {
this.objectId = objectId;
}public String getObjectName() {
return objectName;
}public void setObjectName(String objectName) {
this.objectName = objectName;
}public String getOpName() {
return opName;
}public void setOpName(String opName) {
this.opName = opName;
}public String getOpContent() {
return opContent;
}public void setOpContent(String opContent) {
this.opContent = opContent;
}public String getOpResult() {
return opResult;
}public void setOpResult(String opResult) {
this.opResult = opResult;
}public String getLogTime() {
return logTime;
}public void setLogTime(String logTime) {
this.logTime = logTime;
}public String getCategory() {
return category;
}public void setCategory(String category) {
this.category = category;
}public String getRemark() {
return remark;
}public void setRemark(String remark) {
this.remark = remark;
}
}
AssistReportMsg 类
package cn.hb.jaxb1;
import javax.xml.bind.annotation.*;
import java.util.List;
/**
-
@author:lwz
-
@date:2019/10/16 20:03
-
@description:
-
@modefiled by:
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {“rmsId”, “department”, “userId”, “userName”,“reason”,“time”,“conclusion”,“attachList”})
@XmlRootElement(name = “report”)
public class AssistReportMsg {private String rmsId;
private String department;
private String userId;
private String userName;
private String reason;
private String time;
private String conclusion;@XmlElementWrapper(name=“attachList”)
@XmlElement(name=“attach”)
private List attachList;public List getAttachList() {
return attachList;
}public void setAttachList(List attachList) {
this.attachList = attachList;
}@Override
public String toString() {
return “AssistReportMsg{” +
“rmsId=’” + rmsId + ‘’’ +
“, department=’” + department + ‘’’ +
“, userId=’” + userId + ‘’’ +
“, userName=’” + userName + ‘’’ +
“, reason=’” + reason + ‘’’ +
“, time=’” + time + ‘’’ +
“, conclusion=’” + conclusion + ‘’’ +
‘}’;
}public String getRmsId() {
return rmsId;
}public void setRmsId(String rmsId) {
this.rmsId = rmsId;
}public String getDepartment() {
return department;
}public void setDepartment(String department) {
this.department = department;
}public String getUserId() {
return userId;
}public void setUserId(String userId) {
this.userId = userId;
}public String getUserName() {
return userName;
}public void setUserName(String userName) {
this.userName = userName;
}public String getReason() {
return reason;
}public void setReason(String reason) {
this.reason = reason;
}public String getTime() {
return time;
}public void setTime(String time) {
this.time = time;
}public String getConclusion() {
return conclusion;
}public void setConclusion(String conclusion) {
this.conclusion = conclusion;
}
}
JAXBUtils 类
package cn.hb.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
/**
-
@author:lwz
-
@date:2019/10/16 19:43
-
@description:
-
JAXBUtils提供读写XML的方法;
-
@modefiled by:
*/
public class JAXBUtils {public static void writeXML(Object obj, File path, Class… clazz) throws JAXBException {
JAXBContext jctx = JAXBContext.newInstance(clazz);
Marshaller marshaller = jctx.createMarshaller();
// 格式化输出,设置换行和缩进,不设置则会显示在一行
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(obj, path);
// 设置控制台输出
marshaller.marshal(obj, System.out);
}public static Object readXML(File path, Class… clazz) throws JAXBException {
JAXBContext jctx = JAXBContext.newInstance(clazz);
Unmarshaller unMarshaller = jctx.createUnmarshaller();
Object obj = unMarshaller.unmarshal(path);return obj;
}
}
JAXBXMLTest 类
package cn.hb.jaxb;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.time.LocalDate;
import java.util.ArrayList;
/**
-
@author:lwz
-
@date:2019/10/16 19:45
-
@description:
-
JAXBXMLTest提供读写测试方法;
-
@modefiled by:
*/
public class JAXBXMLTest {
private static final String XML_PATH_COUNTRY = “./Country.xml”;
private static final String XML_PATH_COUNTRIES = “./Countries.xml”;public static void main(String[] args) throws JAXBException {
Country china = new Country();
china.setName(“中国”);
china.setCapital(“北京”);
china.setPopulation(1400000000);
china.setRank(25);
String fTimeChina = LocalDate.of(1949, 10, 1).toString();
china.setFoundationTime(fTimeChina);Country america = new Country(); america.setName("United States of America"); america.setCapital("New York"); america.setPopulation(300000000); america.setRank(11); String fTimeAmerica = LocalDate.of(1776, 7, 4).toString(); america.setFoundationTime(fTimeAmerica); File xmlFile = new File(XML_PATH_COUNTRY); File xmlFile1 = new File(XML_PATH_COUNTRIES); System.out.println("\n【写入到" + XML_PATH_COUNTRY + "】"); JAXBUtils.writeXML(china, xmlFile, Country.class); System.out.println("\n【写入list到" + XML_PATH_COUNTRIES + "】"); Countries countries = new Countries(); ArrayList<Country> list = new ArrayList<>(); list.add(china); list.add(america); countries.setCountries(list); JAXBUtils.writeXML(countries, xmlFile1, Countries.class); System.out.println("\n【从" + XML_PATH_COUNTRIES + "中读取】"); Countries countriesRead = (Countries) JAXBUtils.readXML(xmlFile1, Countries.class, Country.class); System.out.println(countriesRead); System.out.println("\n【从" + XML_PATH_COUNTRY + "中读取】"); Country countryRead = (Country) JAXBUtils.readXML(xmlFile, Country.class); System.out.println(countryRead);
}
}
运行测试类,结果如下:
遇到的问题:
解决方法:
在类上添加这个注解
@XmlAccessorType(XmlAccessType.FIELD)
表示使用这个类中的 private 非静态字段作为 xml 的序列化的属性或者元素。这里还可以选择属性,也就是使用 set/get 方法来序列化属性或者元素