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> </ ...
随机推荐
- Centos 升级至 OpenSSH 8 rpm包制作
背景 安全部门扫描系统漏洞,OpenSSH 7.9出现漏洞,需升级到8. 使用 rpmbuild 将源码包编译为 rpm包. yum install rpm-build zlib-devel open ...
- contos7下安装redis&redis的主从复制的配置&redis 哨兵(sentinel)
一.centos7下安装redis 1.解压 redis-5.0.5.tar.gz 压缩文件 解压命令为: .tar.gz -C redis 解压后进入 redis 工作目录,进入 redis-5.0 ...
- idou老师教你学Istio11 : 如何用Istio实现流量熔断
在之前的最佳实践中,已经带大家通过一系列的实践任务领略了Istio的无穷魅力.今天,将向大家介绍如何用Istio实现流量熔断. 熔断机制是创建弹性微服务应用程序的重要模式.熔断可以帮助您自由控制故障影 ...
- Educational Codeforces Round 76 (Rated for Div. 2) D
D题 原题链接 题意:就是给你n个怪兽有一个属性(攻击力),m个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天) 思路:因为怪兽是不 ...
- HRNet网络结构
最近正在阅读CVPR2019的论文Deep High-Resolution Representation Learning for Human Pose Estimation. 无奈看论文中的Netw ...
- 2018/7/31 -zznu-oj -问题 C: 磨刀- 【扩展欧几里得算法的基本应用】
问题 C: 磨刀 时间限制: 1 Sec 内存限制: 128 MB提交: 190 解决: 39[提交] [状态] [讨论版] [命题人:admin] 题目描述 磨刀是一个讲究的工作,只能在n℃下进 ...
- 设计模式-模板方法设计模式--Template Method design pattern
/** * Abstract implementation of the {@link org.springframework.context.ApplicationContext} * interf ...
- nginx配置跨域之后每次访问会发送两次请求
公司项目从前后端不分离转到前后端分离 首先遇到的问题就是前后端分离的时候跨域的问题 但是当跨域成功配置并且能访问成功的时候发现 每次客户端的请求都会发送两次 第一次是OPTIONS的请求,然后才是正常 ...
- 第69题:x的平方根
一. 问题描述 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输 ...
- Codeforces Round #469 (Div. 1) 949C C. Data Center Maintenance (Div. 2 950E)
题 OvO http://codeforces.com/contest/949/problem/C codeforces 949C 950E 解 建图,记原图为 G1,缩点,记缩完点后的新图为G2 缩 ...