一、上传xml文件如下图格式,使用XStream进行读取,根据自己需要,可以在<CS></CS>标签中包含多个<BindInfo>标签,和<BindInfo>中绑定多个<Element>标签类似
<?xml version="1.0" encoding="UTF-8"?>
<CS>
<BindInfo>
<zllx>0</zllx>
<Element>
<pzmc>3</pzmc>
<pzlx>bit</pzlx>
</Element>
<Element>
<pzmc>3</pzmc>
<pzlx>bit</pzlx>
</Element>
</BindInfo>
</CS>
二、引入pom依赖
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
三、controller层 接收文件
@PostMapping("upload")
public Map<String, Object> upload(MultipartFile file) {
Map<String, Object> map = service.upload(file);
return map;
}
四、service层,主要代码为?stream.xmlToBean。utils的封装方法在最后
public Map<String, Object> upload(MultipartFile file) {
Map<String, Object> result = new HashMap<>();
XStreamUtil stream = new XStreamUtil();
BindInfo info = new BindInfo();
try {
//调用Util的封装方法
info = stream.xmlToBean(file.getInputStream(), BindInfo.class);
} catch (IOException e) {
e.printStackTrace();
log.error("文件格式有误");
result.put("code", -1);
result.put("message", "文件格式有误");
return result;
}
List<ZlzdEntity> bindInfo = info.getBindInfo();
for (ZlzdEntity : bindInfo) {
insert(zlzdEntity);//自己写的插入
}
result.put("code", 200);
result.put("message", "上传成功");
return result;
}
五、实体类
- 首先最外层实体,与<CS>标签对应
? ? @XStreamAlias? 指定被注解的元素在xml文件中的标签名, 将属性按照别名输出,
? ? @XStreamImplicit?指定被注解的List被映射成多个标签
package com.tlck.wxgl.entity.xml;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.tlck.wxgl.entity.ykxx.WxglZlzdEntity;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
@XStreamAlias("CS")//最外层标签
@JsonIgnoreProperties(value = "handler")
public class BindInfo {
@XStreamImplicit(itemFieldName = "BindInfo")//里面标签
private List<ZlzdEntity> BindInfo = new ArrayList<ZlzdEntity>();
}
Element标签如下映射,注解对应好即可
@Data
@AllArgsConstructor
@NoArgsConstructor
@XStreamAlias("BindInfo")
@JsonIgnoreProperties(value = {"handler"})
public class ZlzdEntity {
@XStreamImplicit(itemFieldName = "Element")
private List<ZlzdpzEntity> pzList ;
}
六、XStreamUtil 封装xml转bean如下
/**
* @description: xml工具类
*/
public class XStreamUtil {
private XStream xStream = this.getXStreamInstance();
private XStream getXStreamInstance() {
return new XStream(new DomDriver(null, new XmlFriendlyNameCoder("_-", "_"))) {
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
return new MapperWrapper(next) {
@Override
public boolean shouldSerializeMember(Class definedIn, String fieldName) {
if (definedIn == Object.class) {
try {
return this.realClass(fieldName) != null;
} catch (Exception e) {
return false;
}
} else {
return super.shouldSerializeMember(definedIn, fieldName);
}
}
};
}
};
}
/**
* xml转实体
*/
public <T> T xmlToBean(InputStream file, Class<T> cls) throws IOException, XStreamException {
this.xStream.processAnnotations(cls);//对指定的类使用Annotations 进行序列化
T t = (T) this.xStream.fromXML(file);//转实体类bean
return t;
}
}
?
|