多单位版国产化地质资料管理系统
zs
2025-12-18 4f0d9bde31a80f6279e26466250da7716eec627f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.zbooksoft.gdmis.common;
 
 
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.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
/**
 * @Description: 解析xml工具类
 * @Author: zhai
 * @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;
        try {
            SAXReader saxReader = new SAXReader();
            File file = new File(path);
            xmlStream = new FileInputStream(file);
            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();
        }
    }
 
    /**
     * 获取xml文件的某个节点的内容
     *
     * @param document
     * @param nodeName 节点名称
     * @return
     * @throws Exception
     */
    public static Node getNode(Document document, String nodeName) throws Exception {
        try {
            return document.selectSingleNode("//" + nodeName);
        } catch (Exception e) {
            throw new Exception("解析xml文件失败");
        }
    }
 
    /**
     * 获取节点的文本内容
     *
     * @param node
     * @param nodeName
     * @return
     * @throws Exception
     */
    public static String getNodeText(Node node, String nodeName) throws Exception {
        try {
            return node.valueOf(nodeName);
        } catch (Exception e) {
            throw new Exception("解析xml节点内容失败");
        }
    }
 
    public static List<Node> getNodeList(Node node, String nodeName) throws Exception {
        try {
            List<Node> list = node.selectNodes(nodeName);
            return list;
        } catch (Exception e) {
            throw new Exception("解析xml节点内容失败");
        }
    }
 
    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 {
            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();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
 
    }
 
 
}