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
JAXB序列化对象与反序列化XML的更多相关文章
- redis整合Spring之序列化对象与反序列化
写在最前面 1.Spring必须是4.2.6及以上版本才支持redis 2.jar包版本建议统一 需要准备jar包 1.aopalliance-1.0.jar 2.spring-data-common ...
- python序列化对象和反序列化
1.首先不管哪种语言都会用到序列化和反序列化的过程, 2.序列化:把对象转换为字节序列的过程称为对象的序列化: 反序列化:把对象转换为字节序列的过程称为对象的序列化. 3.序列化的作用:把对象(变 ...
- Android中序列化对象到XMl 和 XML反序列化为对象
package com.example.xmloperation; import java.io.File; import java.io.FileOutputStream; import java. ...
- 对类参数的序列化和反序列化XML
/// <summary> /// Xml序列化与反序列化 /// </summary> public class XmlUtil { #region 反序列化 /// < ...
- C# 序列化反序列化XML的帮助类
以下是一个包装的用于序列化反序列化XML和C# 对象的类. public class XmlSerializeHelper<T> { #region Serial ...
- 序列化对象为xml字符串
/// <summary> /// 序列化对象为xml字符串 /// </summary> /// <param name="obj" ...
- C# 使用XML序列化对象(二)
在C# 使用XML序列化对象(一)中描述了使用XML序列化对象的最简单的实现. 现在我们来看看稍微复杂一点的情况: 现有两个类:A和B,B是A的派生类,如下所示: public class A { p ...
- C# 使用XML序列化对象(一)
在System.Xml.Serialization命名空间中提供了XML序列化类XmlSerializer用于将对象序列化为XML. 下面看一个最简单的例子: public class A { pub ...
- c#xml序列化对象,xml标记都缩写了
最近最后一个接口,他们的格式很严格必须是如下格式 <message> <age>20</age> <name>张三</name> </ ...
随机推荐
- linux下安装nginx+php+mysql环境 详细教程
话不多说上代码 linux环境:centos 7.0 64位 nginx:nginx-1.8.0.tar.gz php: php-7.1.1.tar.gz mysql: mysql-5.6.21.ta ...
- jquery进行each遍历时,根据条件取消某项操作
示例代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- 大数据之路week05--day02(Maven安装,环境变量的配置及基本使用)
今天我们就来学习一下maven,怎么说呢,maven更像是一种管理的工具,实现的原理是使用插件. 举个例子,比如说,一个公司需要做一个项目,这个项目又分成了很多的模块,每个模块又分成了许多的业务等等, ...
- [Javascript] Create an Image with JavaScript Using Fetch and URL.createObjectURL
Most developers are familiar with using img tags and assigning the src inside of HTML. It is also po ...
- 慕课网SSMOA办公系统
目录 需求分析 1 用例图 系统设计 包及全局配置 数据库设计 工具类 具体功能实现 1 dao层功能实现 2 编码过滤器及登陆拦截器 3 单元测试 遇到的问题总结 1 新建一个Modul不会打开新页 ...
- ES6-21.class基本语法
1.简介(详情参考) class是构造函数的语法糖. class的constructor方法内的实现,就是原来构造函数的实现. class内的所有方法都是在prototype上的,就是原来构造函数的p ...
- Lavevel 中 trait 如何继承与复写
1 写一个基类 2 基类中 use YourTrait 3 写一个子类 extends 基类 4 子类中覆写 YourTrait 中的同名方法 $query = parent::scopeOfPara ...
- RabbitMq、ActiveMq、Kafka和Redis做Mq对比
转载自:https://blog.csdn.net/qiqizhiyun/article/details/79848834 一.RabbitMq RabbitMQ是一个Advanced Message ...
- 026_编写 nginx 启动脚本
#!/bin/bash#本脚本编写完成后,放置在/etc/init.d/目录下,就可以被 Linux 系统自动识别到该脚本#如果本脚本名为/etc/init.d/nginx,则 #service ng ...
- moment.js 日期转换工具
官方网站: http://momentjs.cn/ 文档: https://itbilu.com/nodejs/npm/4Jxk-Ti-l.html https://www.jianshu.com/p ...