1.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<tNode code="t1" name="动物">
<tNode code="n1" name="狗"></tNode>
<tNode code="n2" name="猫"></tNode>
</tNode>
<tNode code="t2" name="植物">
<tNode code="p1" name="花"></tNode>
<tNode code="p2" name="草"></tNode>
</tNode>
</xml>
2.解析
2.1.实体类
package com.qin.test;
import java.io.Serializable;
import java.util.List;
public class XmlNode implements Serializable {
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
2.2.handler
package com.qin.test;
import org.springframework.beans.BeanUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class XmlSaxHandler<T> extends DefaultHandler {
private List<T> list = new ArrayList<>();
private Class<T> clazz;
private T t;
public XmlSaxHandler() {
}
public XmlSaxHandler(Class clazz) {
this.clazz =clazz;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if ("tNode".equals(qName)) {
try {
t=clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i = 0; i <attributes.getLength() ; i++) {
String qName1 = attributes.getQName(i);
String value = attributes.getValue(i);
Class<?> tClass = t.getClass();
Field[] fields = tClass.getDeclaredFields();
for (Field field : fields) {
if (qName1.equals(field.getName())){
String name = field.getName();
char[] chars = name.toCharArray();
chars[0] -= 32;
String tMethodName = "set" + String.valueOf(chars);
Method method = null;
try {
method = tClass.getMethod(tMethodName, field.getType());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
method.invoke(t,value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
list.add(t);
}
}
}
2.3.解析工具
package com.qin.common.util;
import com.alibaba.fastjson.JSONObject;
import com.qin.test.XmlNode;
import com.qins.test.XmlSaxHandler;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class XmlParserUtil {
public static <T> List<T> parse(File file, Class<T> clazz) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XmlSaxHandler handler = new XmlSaxHandler(clazz);
parser.parse(file, handler);
return handler.getList();
}
public static <T> List<T> parse(MultipartFile multipartFile, Class<T> clazz) throws Exception {
File tempFile=null;
try {
String filename = multipartFile.getOriginalFilename();
InputStream inputStream = multipartFile.getInputStream();
tempFile = new File(UUID.randomUUID().toString().replaceAll("-", "") + "-" + filename);
inputStreamToFile(inputStream, tempFile);
return parse(tempFile, clazz);
} finally {
if(null!=tempFile){
File del = new File(tempFile.toURI());
del.delete();
}
}
}
private static void inputStreamToFile(InputStream ins, File file) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ins.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
3.测试
3.1.main测试
public static void main(String[] args) throws Exception {
File file = new File("D:\\test.xml");
List<XmlNode> list = XmlParserUtil.parse(file, XmlNode.class);
System.out.println(JSONObject.toJSONString(list));
}
3.2.接口测试
@PostMapping(value = "/testParseXml")
public void testParseXml(MultipartFile file) throws Exception {
List<XmlNode> result = XmlParserUtil.parse(file, XmlNode.class);
System.out.println(JSONObject.toJSONString(result));
}