读取xml文件转成Map并校验必选项是否包含

工具类编写如下

import com.alibaba.fastjson.JSONObject;
import org.dom4j.Attribute;
import org.dom4j.io.SAXReader;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.*;
import java.util.concurrent.ExecutorService;

import static javax.xml.bind.DatatypeConverter.parseString;

/**
 * @auther mxLiu
 * @create 2022-03-03 19:22
 */
public class ReadXML {
    /*
     * 解析XML文件,返回节点信息
     */
    public static Map getJSONfromXML(MultipartFile mFile) throws IOException, DocumentException {
        // 读取XML文件内容
        BufferedReader br = new BufferedReader(new InputStreamReader(mFile.getInputStream(), "utf-8"));
        StringBuffer buffer = new StringBuffer();
        String line = " ";
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        InputStream stream = new ByteArrayInputStream(buffer.toString().getBytes());
        BufferedInputStream input = new BufferedInputStream(stream);
        List list;
        try {
            Document document = new SAXReader().read(input);
            Element root = document.getRootElement();
            list = root.elements();
        }catch (org.dom4j.DocumentException exception){
            return new HashMap();
        }
        return toMap(list);
    }

    public static Map toMap(List elementList){
        Map pointMap = new HashMap();
        List pointList = new ArrayList();
        List errorList = new ArrayList();
        for (Object o : elementList) {
            Map map = new HashMap();
            Element element;
            try {
                element = (Element) o;
            }catch (Exception e){
                //格式不对
                return new HashMap();
            }
            Attribute deviceId = element.attribute("device_id");
            Attribute deviceName = element.attribute("device_name");
            Attribute mainDeviceId = element.attribute("main_device_id");
            Attribute mainDeviceName = element.attribute("main_device_name");
            if (deviceId==null){
                errorList.add("device_id");
            }
            if (deviceName==null){
                errorList.add("device_name");
            }
            if (mainDeviceId==null){
                errorList.add("main_device_id");
            }
            if (mainDeviceName==null){
                errorList.add("main_device_name");
            }
            if (errorList.isEmpty()) {
                map.put("device_id", deviceId.getValue());
                map.put("device_name", deviceName.getValue());
                map.put("bay_id", element.attribute("bay_id").getValue());
                map.put("bay_name", element.attribute("bay_name").getValue());
                map.put("main_device_id", mainDeviceId.getValue());
                map.put("main_device_name", mainDeviceName.getValue());
                map.put("component_id",element.attribute("component_id").getValue());
                map.put("component_name", element.attribute("component_name").getValue());
                map.put("device_type", element.attribute("device_type").getValue());
                map.put("meter_type", element.attribute("meter_type").getValue());
                map.put("appearance_type", element.attribute("appearance_type").getValue());
                map.put("save_type_list", element.attribute("save_type_list").getValue());
                map.put("recognition_type_list", element.attribute("recognition_type_list").getValue());
                map.put("phase", element.attribute("phase").getValue());
                map.put("device_info", element.attribute("device_info").getValue());
                map.put("station_name", element.attribute("station_name").getValue());
                map.put("station_code", element.attribute("station_code").getValue());
                map.put("video_pos", element.attribute("video_pos").getValue());
                map.put("data_type", element.attribute("data_type").getValue());

                //模型中不包含的字段
                map.put("point_level", element.attribute("point_level").getValue());
                map.put("dbs_id", element.attribute("dbs_id").getValue());
                map.put("component_dbs_id", element.attribute("component_dbs_id").getValue());
                map.put("deviceid", element.attribute("deviceid").getValue());
                map.put("info", element.attribute("info").getValue());
                map.put("camera_id", element.attribute("camera_id").getValue());
                map.put("preset", element.attribute("preset").getValue());
                pointList.add(map);
            }else {
                //有必填项缺失
                pointMap.put("error",errorList);
                return pointMap;
            }
        }
        pointMap.put("Items",pointList);
        return pointMap;
    }
}

Controller层调用语句如下

if (fileName.endsWith("xml")) {
			// 取xml数据
			Map itemMap = ReadXML.getJSONfromXML(file);
			if (CollectionUtils.isEmpty(itemMap)) {
				return new Result().error("文件内容有误");
			} else if (itemMap.containsKey("error")) {
				List errorList = (List) itemMap.get("error");// 只要有,必不为空,无需再校验
				StringBuilder sb = new StringBuilder();
				sb.append("导入错误!点位信息缺失必填项:");
				for (Object o : errorList) {
					sb.append(o);
					sb.append("/");
				}
				sb.deleteCharAt(sb.lastIndexOf("/"));
				return new Result().error(sb.toString());
			}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值