| | |
| | | package com.zbooksoft.gdmis.common; |
| | | |
| | | |
| | | import org.dom4j.*; |
| | | import com.zbooksoft.gdmis.controller.ArchivesCustomConfigController; |
| | | import org.dom4j.Document; |
| | | import org.dom4j.Element; |
| | | import org.dom4j.Node; |
| | | import org.dom4j.XPath; |
| | | import org.dom4j.io.SAXReader; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.io.*; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | import static gnu.cajo.utils.extra.TransparentItemProxy.handler; |
| | | |
| | | /** |
| | | * @Description: 解析xml工具类 |
| | |
| | | * @Date: 2024/8/6 |
| | | **/ |
| | | public class XmlUtil { |
| | | private static final Logger logger = LoggerFactory.getLogger(XmlUtil.class); |
| | | |
| | | |
| | | /** |
| | | * 通过 Node 查找子节点 |
| | | * |
| | | * @param node Node |
| | | * @param path xpath表达式 |
| | | * @return Node元素 |
| | | * @throws Exception |
| | | */ |
| | | public static Node getNode(Node node, String path) { |
| | | XPath xPath = node.createXPath(path); |
| | | return xPath.selectSingleNode(node); |
| | | } |
| | | |
| | | public static Document getDocument(String path) throws Exception { |
| | | FileInputStream xmlStream = null; |
| | |
| | | Document document = saxReader.read(xmlStream); |
| | | return document; |
| | | } catch (Exception e) { |
| | | throw new Exception("解析xml文件失败"); |
| | | } finally { |
| | | xmlStream.close(); |
| | | } |
| | | } |
| | | |
| | | public static Document getDocument(MultipartFile file) throws Exception { |
| | | FileInputStream xmlStream = null; |
| | | try { |
| | | // 1. 创建临时文件 |
| | | File tempFile = File.createTempFile("temp_", ".tmp", new File(System.getProperty("java.io.tmpdir"))); |
| | | file.transferTo(tempFile); |
| | | xmlStream = new FileInputStream(tempFile); |
| | | SAXReader saxReader = new SAXReader(); |
| | | Document document = saxReader.read(xmlStream); |
| | | if (tempFile.exists()) { |
| | | tempFile.delete(); |
| | | } |
| | | return document; |
| | | } catch (Exception e) { |
| | | throw new Exception("解析xml文件失败"); |
| | | } finally { |
| | | xmlStream.close(); |
| | |
| | | } |
| | | } |
| | | |
| | | public static Integer stringToInteger(String str) throws Exception { |
| | | try { |
| | | return Integer.valueOf(str); |
| | | } catch (NumberFormatException e) { |
| | | logger.info("字符串转Integer异常:{}", str); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | public static Double stringToDouble(String str) { |
| | | try { |
| | | return Double.valueOf(str); |
| | | } catch (NumberFormatException e) { |
| | | logger.info("字符串转Double异常:{}", str); |
| | | return 0.0; |
| | | } |
| | | } |
| | | |
| | | public static Date stringToDate(String str) throws Exception { |
| | | try { |
| | | |
| | | if (str == null || "".equals(str)) { |
| | | return null; |
| | | } |
| | | if (str.contains("/")) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); |
| | | return sdf.parse(str); |
| | | } |
| | | if (str.contains("-")) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.parse(str); |
| | | } |
| | | return null; |
| | | } catch (NumberFormatException e) { |
| | | logger.info("字符串转日期异常:{}", str); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static String getFileType(String name) { |
| | | |
| | | name = name.toUpperCase(); |
| | | String newName = name; |
| | | switch (name) { |
| | | case "ZW": |
| | | newName = "正文"; |
| | | break; |
| | | case "SP": |
| | | newName = "审批"; |
| | | break; |
| | | case "FT": |
| | | newName = "附图"; |
| | | break; |
| | | case "FB": |
| | | newName = "附表"; |
| | | break; |
| | | case "FJ": |
| | | newName = "附件"; |
| | | break; |
| | | case "DMT": |
| | | newName = "多媒体"; |
| | | break; |
| | | case "SJK": |
| | | newName = "数据库"; |
| | | break; |
| | | case "RJ": |
| | | newName = "软件"; |
| | | break; |
| | | case "QT": |
| | | newName = "其他"; |
| | | break; |
| | | } |
| | | return newName; |
| | | } |
| | | |
| | | |
| | | public static Integer getSecurityCode(String security) { |
| | | Integer code = null; |
| | | switch (security) { |
| | | case "绝密": |
| | | code = 4; |
| | | break; |
| | | case "机密": |
| | | code = 3; |
| | | break; |
| | | case "秘密": |
| | | code = 2; |
| | | break; |
| | | case "内部": |
| | | code = 1; |
| | | break; |
| | | case "不涉密": |
| | | code = 0; |
| | | break; |
| | | } |
| | | return code; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | try { |
| | | System.out.println(new Date()); |
| | | // String path = "E:\\Desktop\\新建文件夹 (4)\\地质资料文件目录.xml"; |
| | | String path = "E:\\Desktop\\新建文件夹 (4)\\2023\\地质资料文.xml"; |
| | | Document document = getDocument(path); |
| | | Node wjjxxNode = getNode(document, "WJJXX"); |
| | | List<Node> cgwjjxxNodeList = getNodeList(wjjxxNode, "CGWJJXX/CGWJXX"); |
| | | for (Node cgwjjxxNode : cgwjjxxNodeList){ |
| | | String cgwjmc = getNodeText(cgwjjxxNode, "WJLB"); |
| | | System.out.println(cgwjmc); |
| | | String filePath = "E:\\Desktop\\新建文件夹 (5)\\b01_0095.ml"; |
| | | try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { |
| | | String line; |
| | | while ((line = br.readLine()) != null) { |
| | | // 处理每一行 |
| | | System.out.println(line); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println(new Date()); |
| | | |
| | | |
| | | } catch (Exception e) { |
| | | System.out.println(e.getMessage()); |
| | | } |