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());
|
}
|
|
}
|
|
|
}
|