JAXB序列化对象与反序列化XML
1、什么是JAXB
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。
该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,
JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
2、JAR下载
http://www.java2s.com/Code/Jar/j/Downloadjaxbapi22jar.htm
3、Demo
解决字段包含子类,list集合序列化
根节点
RequestVO.java
import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) //XML文件中的根标识 @XmlRootElement(name = "request") //控制JAXB 绑定类中属性和字段的排序 @XmlType(propOrder = { "entryOrder", "orderLine" }) /** * 生产XML根节点 * @author Stephen * @Time 2019年6月27日09:49:36 * */ public class RequestVO { private EntryOrder entryOrder; @XmlElementWrapper(name = "orderLines") private List<OrderLine> orderLine; public EntryOrder getEntryOrder() { return entryOrder; } public void setEntryOrder(EntryOrder entryOrder) { this.entryOrder = entryOrder; } public List<OrderLine> getOrderLine() { return orderLine; } public void setOrderLine(List<OrderLine> orderLine) { this.orderLine = orderLine; } }
EntryOrder.java
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) //XML文件中的根标识 @XmlRootElement(name = "entryOrder") //控制JAXB 绑定类中属性和字段的排序 @XmlType(propOrder = { "totalOrderLines", "entryOrderCode", "ownerCode", "warehouseCode", "entryOrderId", "entryOrderType", "outBizCode", "confirmType", "status", "operateTime", "remark" }) /** * 子节点 * @author Stephen * @Time 2019年6月27日09:49:36 * */ public class EntryOrder { public String totalOrderLines; public String entryOrderCode; public String ownerCode; public String warehouseCode; public String entryOrderId; public String entryOrderType; public String outBizCode; public String confirmType; public String status; public String operateTime; public String remark; public EntryOrder() { this.totalOrderLines = ""; this.entryOrderCode = ""; this.ownerCode = ""; this.warehouseCode = ""; this.entryOrderId = ""; this.entryOrderType = ""; this.outBizCode = ""; this.confirmType = ""; this.status = ""; this.operateTime = ""; this.remark = ""; } public String getTotalOrderLines() { return totalOrderLines; } public void setTotalOrderLines(String totalOrderLines) { this.totalOrderLines = totalOrderLines; } public String getEntryOrderCode() { return entryOrderCode; } public void setEntryOrderCode(String entryOrderCode) { this.entryOrderCode = entryOrderCode; } public String getOwnerCode() { return ownerCode; } public void setOwnerCode(String ownerCode) { this.ownerCode = ownerCode; } public String getWarehouseCode() { return warehouseCode; } public void setWarehouseCode(String warehouseCode) { this.warehouseCode = warehouseCode; } public String getEntryOrderId() { return entryOrderId; } public void setEntryOrderId(String entryOrderId) { this.entryOrderId = entryOrderId; } public String getEntryOrderType() { return entryOrderType; } public void setEntryOrderType(String entryOrderType) { this.entryOrderType = entryOrderType; } public String getOutBizCode() { return outBizCode; } public void setOutBizCode(String outBizCode) { this.outBizCode = outBizCode; } public String getConfirmType() { return confirmType; } public void setConfirmType(String confirmType) { this.confirmType = confirmType; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getOperateTime() { return operateTime; } public void setOperateTime(String operateTime) { this.operateTime = operateTime; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
OrderLine.java
import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) //XML文件中的根标识 @XmlRootElement(name = "orderLine") //控制JAXB 绑定类中属性和字段的排序 @XmlType(propOrder = { "outBizCode", "orderLineNo", "ownerCode", "itemCode", "itemId", "itemName", "inventoryType", "imperfectGrade", "planQty", "actualQty", "batchCode", "productDate", "expireDate", "produceCode", "batch", "remark" }) public class OrderLine { private String outBizCode; private String orderLineNo; private String ownerCode; private String itemCode; private String itemId; private String itemName; private String inventoryType; private String imperfectGrade; private String planQty; private String actualQty; private String batchCode; private String productDate; private String expireDate; private String produceCode; @XmlElementWrapper(name = "batchs") private List<Batch> batch; private String remark; public OrderLine() { this.outBizCode = ""; this.orderLineNo = ""; this.ownerCode = ""; this.itemCode = ""; this.itemId = ""; this.itemName = ""; this.inventoryType = ""; this.imperfectGrade = ""; this.planQty = ""; this.actualQty = ""; this.batchCode = ""; this.productDate = ""; this.expireDate = ""; this.produceCode = ""; this.remark = ""; } public List<Batch> getBatchs() { return batch; } public void setBatchs(List<Batch> batchs) { this.batch = batchs; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getOutBizCode() { return outBizCode; } public void setOutBizCode(String outBizCode) { this.outBizCode = outBizCode; } public String getOrderLineNo() { return orderLineNo; } public void setOrderLineNo(String orderLineNo) { this.orderLineNo = orderLineNo; } public String getOwnerCode() { return ownerCode; } public void setOwnerCode(String ownerCode) { this.ownerCode = ownerCode; } public String getItemCode() { return itemCode; } public void setItemCode(String itemCode) { this.itemCode = itemCode; } public String getItemId() { return itemId; } public void setItemId(String itemId) { this.itemId = itemId; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public String getInventoryType() { return inventoryType; } public void setInventoryType(String inventoryType) { this.inventoryType = inventoryType; } public String getImperfectGrade() { return imperfectGrade; } public void setImperfectGrade(String imperfectGrade) { this.imperfectGrade = imperfectGrade; } public String getPlanQty() { return planQty; } public void setPlanQty(String planQty) { this.planQty = planQty; } public String getActualQty() { return actualQty; } public void setActualQty(String actualQty) { this.actualQty = actualQty; } public String getBatchCode() { return batchCode; } public void setBatchCode(String batchCode) { this.batchCode = batchCode; } public String getProductDate() { return productDate; } public void setProductDate(String productDate) { this.productDate = productDate; } public String getExpireDate() { return expireDate; } public void setExpireDate(String expireDate) { this.expireDate = expireDate; } public String getProduceCode() { return produceCode; } public void setProduceCode(String produceCode) { this.produceCode = produceCode; } }
Batch.java
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) //XML文件中的根标识 @XmlRootElement(name = "batch") //控制JAXB 绑定类中属性和字段的排序 @XmlType(propOrder = { "batchCode", "productDate", "expireDate", "produceCode", "inventoryType", "imperfectGrade", "actualQty" }) public class Batch { private String batchCode; private String productDate; private String expireDate; private String produceCode; private String inventoryType; private String imperfectGrade; private String actualQty; public Batch() { this.batchCode = ""; this.productDate = ""; this.expireDate = ""; this.produceCode = ""; this.inventoryType = ""; this.imperfectGrade = ""; this.actualQty = ""; } public String getBatchCode() { return batchCode; } public void setBatchCode(String batchCode) { this.batchCode = batchCode; } public String getProductDate() { return productDate; } public void setProductDate(String productDate) { this.productDate = productDate; } public String getExpireDate() { return expireDate; } public void setExpireDate(String expireDate) { this.expireDate = expireDate; } public String getProduceCode() { return produceCode; } public void setProduceCode(String produceCode) { this.produceCode = produceCode; } public String getInventoryType() { return inventoryType; } public void setInventoryType(String inventoryType) { this.inventoryType = inventoryType; } public String getImperfectGrade() { return imperfectGrade; } public void setImperfectGrade(String imperfectGrade) { this.imperfectGrade = imperfectGrade; } public String getActualQty() { return actualQty; } public void setActualQty(String actualQty) { this.actualQty = actualQty; } }
JAXBUtil.java
import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; /** * @Description:xml和对象相互转换工具类 * @author:yaolong * @data:2017年2月9日 下午10:46:57 * @version:1.0 */ public class JAXBUtil { /** * JavaBean转换成xml 默认编码UTF-8 * * @param obj * @param writer * @return */ public static String convertToXml(Object obj) { return convertToXml(obj, "UTF-8"); } /** * JavaBean转换成xml * * @param obj * @param encoding * @return */ public static String convertToXml(Object obj, String encoding) { String result = null; try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xml头信息 StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * xml转换成JavaBean * @param xml * @param c * @return * @throws JAXBException */ @SuppressWarnings("unchecked") public static <T> T converyToJavaBean(String xml, Class<T> c) throws JAXBException { T t = null; JAXBContext context = JAXBContext.newInstance(c); Unmarshaller unmarshaller = context.createUnmarshaller(); t = (T) unmarshaller.unmarshal(new StringReader(xml)); return t; } }
XML格式
<?xml version="1.0" encoding="utf-8"?> <request> <entryOrder> <totalOrderLines></totalOrderLines> <entryOrderCode></entryOrderCode> <ownerCode></ownerCode> <warehouseCode></warehouseCode> <entryOrderId></entryOrderId> <entryOrderType></entryOrderType> <outBizCode></outBizCode> <confirmType></confirmType> <status></status> <operateTime></operateTime> <remark></remark> </entryOrder> <orderLines> <orderLine> <outBizCode></outBizCode> <orderLineNo></orderLineNo> <ownerCode></ownerCode> <itemCode></itemCode> <itemId></itemId> <itemName></itemName> <inventoryType></inventoryType> <imperfectGrade></imperfectGrade> <planQty></planQty> <actualQty></actualQty> <batchCode></batchCode> <productDate></productDate> <expireDate></expireDate> <produceCode></produceCode> <batchs> <batch> <batchCode></batchCode> <productDate></productDate> <expireDate></expireDate> <produceCode></produceCode> <inventoryType></inventoryType> <imperfectGrade></imperfectGrade> <actualQty></actualQty> </batch> </batchs> <remark></remark> </orderLine> </orderLines> </request>
ps:此示例都是针对于字符串的操作 ,重要的是Util
不忘初心