• Java上传且后台解析XML文件


    后台代码:

    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    /**
     * 解析XML文件
     * @author 【】
     *
     */
    public class ReaderXML {
    
    	@RequestMapping(value = "", method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
    	public void parseXML(HttpServletRequest request) {
    		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    		MultipartFile mFile = multipartRequest.getFile("xml");
    		// 获取传入的xml路径
    		String url = mFile.getOriginalFilename();
    		// 获取传入的文件名称
    		String hzmc = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf("\") + 1);
    		// 解析XML文件,返回节点信息集合
    		NodeList childNodes = getNodeLists(mFile);
    		for (int i = 0; i < childNodes.getLength(); i++) {
    			// 获取单个节点信息
    			Node node = childNodes.item(i);
    			if ("节点名".equals(node.getNodeName())) {
    				String 节点值 = node.getTextContent();
    				// 获取该节点下子节点
    				NodeList childNodes1 = node.getChildNodes();
    			}
    		}	
    	}
    	/*
    	 * 解析XML文件,返回节点信息
    	 */
    	private NodeList getNodeLists(MultipartFile mFile) {
    		// 读取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());
    		// 返回documentBuilderFactory对象
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		// 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象
    		DocumentBuilder db = dbf.newDocumentBuilder();
    		Document document = db.parse(stream);
    		Element element = document.getDocumentElement();
    		NodeList childNodes = element.getChildNodes();
    		
    		return childNodes;
    	}
    }
    
  • 相关阅读:
    五小步让VS Code支持AngularJS智能提示
    AngularJS----服务,表单,模块
    AJAX 动态加载后台数据 绑定select
    连接mysql 报错 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    MAC中向阿里云服务器上传文件
    使用Navicat连接阿里云ECS服务器上的MySQL数据库
    mysql面试题:字段中@之前字符相同且大于等于2条的所有记录
    2018 最新手机号正则(最新最全)
    php同一个用户同时只能登陆一个, 后登陆者踢掉前登陆者(排他登陆)
    php 单冒号 、双冒号的用法
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/9915502.html
Copyright © 2020-2023  润新知