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> </ ...
随机推荐
- 用Python来使用科大讯飞语音识别,so easy
在人工智能高速发展的今天,语音识别技术被带入到人们的工作和生活中,开始被越来越多的人关注和使用,今天,当各种在线客服被机器人客服代替,当速记翻译馆被语音识别代替,甚至当收银员.驾驶员.工厂工人.普通文 ...
- Django:总结setting中的配置
一.Django setting配置说明 二.setting配置一览 一.Django setting配置说明 1.基础 DJANGO_SETTING_MODULE环境变量:让settings模块被包 ...
- idou老师教你学Istio 20 : Istio全景监控与拓扑
根据Istio官方报告,Observe(可观察性)为其重要特性.Istio提供非侵入式的自动监控,记录应用内所有的服务. 我们知道在Istio的架构中,Mixer是管理和收集遥测信息的组件.每一次当请 ...
- 火狐新版移除developer Toolbar和无法关闭自动更新的解决
随着火狐的不断更新已经更新到66版本了,近期注意到有个问题是火狐经常提示更新,更新了没多久,又时不时跳出更新的提示,不胜其烦. 在火狐的前期的版本中(大概4年之前吧)在Options菜单里是可以设置从 ...
- 8种主流NoSQL数据库对比
摘要:虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只是时间问题:被迫使用关系数据库,但最终发现不能适应需求的情况不胜枚举. 简介 NoSQL,是一项全新的数据库革命 ...
- IdentityServer(三)密码模式
前言 用户名密码模式相较于客户端凭证模式,多了用户.通过用户的用户名和密码向Identity Server申请访问令牌.密码模式有两种实现方式. 1.把用户写进内存Identity从中读取账号密码验证 ...
- ADO.Net数据库连接字符串、DbProviderFactory
一.ADO.Net数据库连接字符串 1.OdbcConnection(System.Data.Odbc) (1)SQL Sever 标准安全:" Driver={SQL Server}; S ...
- Maven如何上传ja包到远程仓库
本文转载自沧海一屌丝的博客 https://blog.csdn.net/qq_31924435/article/details/53745811 mvn install 会将项目生成的构件安装到本地 ...
- jQuery获取父窗口的元素
js获取父页面的元素可以用 $(window.parent.document).find("#customer_id").val();这里的customer_id表示父页面某一个元 ...
- Ubuntu Linux使用sudo命令搭建java环境
搬运stackoverflow 注意,以下所有命令需要在root权限下执行 1. 在Ubuntu下打开终端命令或用ssh连接到linux. 2. 更新仓库(只有Ubuntu17.4及以下系统可用): ...