1. 技术目标
- 目标01:如何组装xml的vo对象
- 目标02:ObjectMapper和XmlMapper类的熟练使用
- 目标03:POJO2XML
- 目标04:XML2POJO
2. 技术介绍
最近在公司做一个对接开发票系统的工作,工作也刚好一年了,原本以为会是一件很简单的事情,更不会花费多久的时间,但是当我打开对接公司发的接口文档,我的眼前感到了一阵困惑,怎么数据的传输都是XML的格式呢?之前我曾经写过一篇后台数据传输-JSON,介绍了各种JSON数据格式的传输,而XML是进公司这么久以来,第一次接触这样的接口,只能说明我的水平太cai了!在网上搜索和很久,发现大多都是通过[DOM、SAX、DOM4J、JDOM、Xstream],自己也按照尝试了多次,虽然可以实现数据的组转,但是这也太kegndie了吧!幸好在公司认识一个马哥的同事,在他的指点下,完成了第一个demo的实现,今天闲来无事,在此和大家分享一下,愿共同进步!话不多说,直接开始上代码了。
3. 环境配置
3.1 开发配置
- JDK 1.8 或更高版本
- Maven 3 或更高版本
- MySQL Server 5.6
3.2 技术栈
- Spring Boot
- Jackson XML annotations
- MySQL
4. 技术实践
4.1 创建项目
4.2 创建结构
4.2.1 pom.xml引入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
4.2.2 属性文件配置
4.3 主要代码
4.3.1 XML数据格式
<?xml version='1.0' encoding='UTF-8'?>
<root>
<header/>
<body id="1" size="2">
<datas>
<data k6="22">
<k1>k1</k1>
<k3>
<ki>1</ki>
<ki>2</ki>
</k3>
<k5>1</k5>
<k5>2</k5>
<k7>a7b</k7>
<k8>
<a>a7b</a>
</k8>
<k9>
<![CDATA[java]]>
</k9>
<k2>12</k2>
<k4>
<ka>1</ka>
<ka>2</ka>
</k4>
</data>
<data>
<k1/>
<k5/>
<k7/>
<k8/>
<k9/>
<k2/>
</data>
</datas>
</body>
</root>
4.3.2 创建VO对象
package cn.hacz.edu.vo;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "root")
public class Root {
private Header header;
private Body body;
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
}
package cn.hacz.edu.vo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Header {
}
package cn.hacz.edu.vo;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Body {
@JacksonXmlProperty(isAttribute = true, localName = "id")
private String id;
@JacksonXmlProperty(isAttribute = true, localName = "size")
private Integer size;
/**
* List集合形式
*/
@JacksonXmlElementWrapper(localName = "datas")
@JacksonXmlProperty(localName = "data")
private List<Data> datas;
public List<Data> getDatas() {
return datas;
}
public void setDatas(List<Data> datas) {
this.datas = datas;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getSize() {
return getDatas().size();
}
public void setSize(Integer size) {
this.size = size;
}
}
package cn.hacz.edu.vo;
import java.util.Collection;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Data {
private String k1;
@JacksonXmlProperty(localName = "k2")
private Integer k2;
@JacksonXmlElementWrapper(localName = "k3")
private Collection<String> ki;
@JacksonXmlElementWrapper(localName = "k4")
@JacksonXmlProperty(localName = "ka")
private Collection<String> k4;
@JacksonXmlElementWrapper(localName = "k5", useWrapping = false)
private Collection<String> k5;
@JacksonXmlProperty(isAttribute = true)
private Integer k6;
private DataValue01 k7;
private DataValue02 k8;
// 序列化时是否总是使用 CDATA 块
@JacksonXmlCData(value = true)
private String k9;
public String getK1() {
return k1;
}
public void setK1(String k1) {
this.k1 = k1;
}
public Integer getK2() {
return k2;
}
public void setK2(Integer k2) {
this.k2 = k2;
}
public Collection<String> getKi() {
return ki;
}
public void setKi(Collection<String> ki) {
this.ki = ki;
}
public Collection<String> getK4() {
return k4;
}
public void setK4(Collection<String> k4) {
this.k4 = k4;
}
public Collection<String> getK5() {
return k5;
}
public void setK5(Collection<String> k5) {
this.k5 = k5;
}
public Integer getK6() {
return k6;
}
public void setK6(Integer k6) {
this.k6 = k6;
}
public DataValue01 getK7() {
return k7;
}
public void setK7(DataValue01 k7) {
this.k7 = k7;
}
public DataValue02 getK8() {
return k8;
}
public void setK8(DataValue02 k8) {
this.k8 = k8;
}
public String getK9() {
return k9;
}
public void setK9(String k9) {
this.k9 = k9;
}
}
package cn.hacz.edu.vo;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
public class DataValue01 {
@JacksonXmlText
private String a;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
package cn.hacz.edu.vo;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
public class DataValue02 {
@JacksonXmlText(value = false)
private String a;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
4.3.3 ObjectMapper和XmlMapper
public static XmlMapper getxmlMapper() {
// 重要:创建XmlMapper; 它将使用适当的工厂,解决方法。
XmlMapper xmlMapper = new XmlMapper();
// 格式化代码
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
// <?xml version='1.0' encoding='UTF-8'?>
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
return xmlMapper;
}
4.3.4 POJO2XML
String writeValueAsString = getxmlMapper().writeValueAsString(root);
System.out.println(writeValueAsString);
4.3.5 XML2POJO
File file = new File("D:\\1.xml");
// 02、XML文件转成POJO对象
Root readValue = getxmlMapper().readValue(file, Root.class);
List<Data> datas2 = readValue.getBody().getDatas();
for (Data data : datas2) {
System.out.println(data.getKi());
}
解析xml按照一定顺序:@JsonPropertyOrder({"classStr","size"})
5. 技术总结
5.1
6. 代码下载
本章以及后续章节的源码地址我都会分享出来,请大家自行下载以及git clone。
SpringBoot相关系列文章请访问:目录:SpringBoot学习目录