001package top.cenze.utils.file; 002 003import cn.hutool.core.util.ObjectUtil; 004import cn.hutool.core.util.StrUtil; 005import cn.hutool.core.util.XmlUtil; 006import lombok.SneakyThrows; 007import org.w3c.dom.Document; 008import org.w3c.dom.Element; 009import org.w3c.dom.Text; 010 011import java.io.File; 012import java.util.UUID; 013 014public class XMLUtil { 015 private static final String EXCEL_TEMPLATE_DIR = "template/"; 016 017 // ****************************** 创建 ******************************** 018 019 public static Document createDocument() { 020 Document doc = XmlUtil.createXml(); 021 doc.setXmlStandalone(true); 022 doc.setXmlVersion("1.0"); 023 024 return doc; 025 } 026 027 public static Element createElement(Document doc, String name, String data) { 028 Text text = doc.createTextNode(name); 029 text.setData(data); 030 Element element = doc.createElement(name); 031 element.appendChild(text); 032 033 return element; 034 } 035 036 public static String toXml(Document doc, Element head, Element param, String charset) { 037 Element root = doc.createElement("root"); 038 root.appendChild(head); 039 root.appendChild(param); 040 041 doc.appendChild(root); 042 043 if (StrUtil.isEmpty(charset)) { 044 charset = "UTF-8"; 045 } 046 047 if ("GBK".equals(charset)) { 048 return XmlUtil.toStr(doc, "GBK", true); 049 } else { 050 return XmlUtil.toStr(doc); 051 } 052 } 053 054 055 // ****************************** 解释 ******************************** 056 057 public static Document toXmlDoc(String xmlStr) { 058 Document doc = XmlUtil.parseXml(xmlStr); 059 060 return doc; 061 } 062 063 public static String getElementValue(Document doc, String name) { 064 if (null == doc) { 065 return null; 066 } 067 if (StrUtil.isEmpty(name)) { 068 return null; 069 } 070 071 return doc.getElementsByTagName(name).item(0) 072 .getFirstChild().getNodeValue(); 073 } 074 075 public static String toStr(Document xml) { 076 if (ObjectUtil.isNull(xml)) { 077 return null; 078 } 079 080 return XmlUtil.toStr(xml); 081 } 082 083 @SneakyThrows 084 public static byte[] toBytes(Document xml) { 085 String str = toStr(xml); 086 if (StrUtil.isNotEmpty(str)) { 087 str.getBytes("UTF-8"); 088 } 089 090 return null; 091 } 092 093 @SneakyThrows 094 public static File toFile(Document xml) { 095 if (ObjectUtil.isNull(xml)) { 096 return null; 097 } 098 099 String templateFileName = UUID.randomUUID().toString().replaceAll("-", "") + ".xml"; 100 File tmpFile = CZFileUtil.mkFileToResource(EXCEL_TEMPLATE_DIR, templateFileName); 101 XmlUtil.toFile(xml, tmpFile.getPath()); 102 103// String templateFileName = UUID.randomUUID().toString().replaceAll("-", "") + ".xml"; 104// File tmpFile = CZFileUtil.mkFileToResource(EXCEL_TEMPLATE_DIR, templateFileName); 105// if (!FileUtil.exist(tmpFile)) { 106// throw new NullPointerException("创建文件失败"); 107// } 108// 109// FileOutputStream fos = new FileOutputStream(tmpFile); 110// //设置格式 111// OutputFormat format = new OutputFormat(); 112// format.setIndentSize(4); //缩进为4 113// format.setNewlines(true); //换行 114// format.setTrimText(true); //去除空格 115// format.setEncoding("UTF-8"); 116// XMLWriter xw = new XMLWriter(fos, format); 117// //将整个文档对象写入到文件中 118// xw.write(xml); 119// xw.close(); 120// fos.flush(); 121// fos.close(); 122 123 return tmpFile; 124 } 125}