import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.util.*;
/**
* @author
* @version 1.0
* @Description
* @date 2020/12/26 15:30
*/
public class XmlUtils {
private static final Logger logger = LoggerFactory.getLogger(XmlUtils.class);
/**
* @return
* @Description 生成文件名称
* @Param [prefix] 名称组成成分,用_分割
*/
public static String fileNameFactory(String... prefix) {
String fileName = "";
if (prefix != null && prefix.length > 0) {
for (int i = 0; i < prefix.length; i++) {
fileName += prefix[i];
if (i < prefix.length - 1) {
fileName += "_";
}
}
}
return fileName;
}
/**
* @return
* @Description xml模板转为String
* @Param [fileName] 模板文件名
*/
public static String convertXmlToString(String fileName) {
String str;
try {
InputStream inputStream = getInputStream("/xml_temp/" + fileName + ".xml");
//读取文件信息
str = IOUtils.toString(new InputStreamReader(inputStream, "UTF-8"));
} catch (IOException e) {
logger.error("读取模板失败:{}", fileName, e);
return StringUtils.EMPTY;
}
return str;
}
/**
* @return
* @Description 替换xml属性值
* @Param [xml, replaceLabel, map] xml模板,模板中被替换的位置,替换的值
*/
public static String replaceXml(String xml, Map<String, Object> map) {
if (map != null) {
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String type;
String key = iterator.next();
// 接受报价和接受要输入都是AP
if ((type = (String) map.get(key)).equals("APP")) {
type = "AP";
}
xml = xml.replace(key, type);
}
}
return xml;
}
/**
* @return
* @Description json文件转为map
* @Param [path] 文件路径
*/
public static Map<String, Object> convertJsonToMap(String fileName) {
Map map = new LinkedHashMap();
try {
InputStream inputStream = getInputStream("/xml_temp/" + fileName + ".json");
//读取文件信息
String str = IOUtils.toString(new InputStreamReader(inputStream, "UTF-8"));
//转换Map对象
map = JSONObject.parseObject(str, LinkedHashMap.class);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* @return 文件输入流
* @Description 根据文件相对路径获取输入流
* @Param [fileName] 文件名称
*/
public static InputStream getInputStream(String fileName) {
try {
ClassPathResource classPathResource = new ClassPathResource(fileName);
return classPathResource.getInputStream();
} catch (IOException e) {
logger.error("读取xml模板失败,{}", fileName, e);
}
return null;
}
/**
* @return
* @Description 修改xml标签对应值, 改变原始map
* @Param [map, element, labels]
*/
public static void setElementByLabel(Map<String, Object> map, String element, String... labels) {
int size;
String label = "";
if (labels != null && (size = labels.length) > 0) {
for (int i = 0; i < size; i++) {
label += labels[i];
if (i < size - 1)
label += " --> ";
if (map.get(labels[i]) instanceof Map) {
map = (Map<String, Object>) map.get(labels[i]);
} else {
String value = (String) map.get(labels[i]);
map.put(labels[i], element);
logger.info("\n 标签: {} \n 旧值: {} \n 新值: {}", label, value, element);
}
}
}
}
/**
* @return
* @Description 修改xml标签对应值, 不改变原始map
* @Param [map, label]
*/
public static Map<String, Object> setElementHaveReturnByLabel(Map<String, Object> map, String element, String... labels) {
int size;
Map<String, Object> newMap;
if ((newMap = map) != null && labels != null && (size = labels.length) > 0) {
for (int i = 0; i < size; i++) {
if (newMap.get(labels[i]) instanceof Map) {
newMap = (Map<String, Object>) newMap.get(labels[i]);
} else {
newMap.put(labels[i], element);
}
}
}
return map;
}
/**
* @return
* @Description 爆露对外方法
* @Param [filePath]
*/
public static Map<String, Object> convertMapByXml(String fileName) {
try {
SAXReader saxReader = new SAXReader();
//加载文档到dom4j中的dom中
Document document = saxReader.read(getInputStream("/xml_temp/" + fileName + ".xml"));
//获取文档根节点
Element rootElement = document.getRootElement();
Map<String, Object> mapXml = new LinkedHashMap<>();
// 转map
xmlToMap(mapXml, rootElement);
return mapXml;
} catch (DocumentException e) {
logger.error("获取Document对象失败,文件名:{}", fileName, e.getMessage());
}
return null;
}
/**
* xml转map
*
* @param map
* @param rootElement
*/
private static void xmlToMap(Map<String, Object> map, Element rootElement) {
// 获得当前节点的子节点
List<Element> childElements = rootElement.elements();
// 当前结点没有子结点退出递归
if (childElements.size() > 0) {
Map<String, Object> tempMap = new LinkedHashMap<>();
for (Element e : childElements) {
// 递归获取子节点
xmlToMap(tempMap, e);
map.put(rootElement.getName(), tempMap);
}
} else {
map.put(rootElement.getName(), rootElement.getText());
}
}
/**
* Map转XML
*
* @param map 需要转换的map
* @param parentName 就是map的根key,如果map没有根key,就输入转换后的xml根节点。
* @return String-->XML
*/
public static String convertXmlByMap(Map<String, Object> map,
String parentName) {
Map<String, Object> rootMap = (Map<String, Object>) map.get(parentName);
if (rootMap == null) {
rootMap = map;
}
Document doc = DocumentHelper.createDocument();
//设置根节点
doc.addElement(parentName);
// 组装xml
String xml = mapToXml(doc.getRootElement(), parentName, rootMap);
return formatXML(xml);
}
/**
* 遍历map组装xml
*
* @param element 根节点
* @param parentName 子节点名字
* @param map map数据
* @return String 未格式化的xml
*/
private static String mapToXml(Element element, String parentName,
Map<String, Object> map) {
Element e = element.addElement(parentName);
Set<String> set = map.keySet();
for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
String key = (String) it.next();
// 当前结点包含子结点
if (map.get(key) instanceof Map) {
// 递归遍历出所有子结点
mapToXml(e, key, (Map<String, Object>) map.get(key));
// 预留 --- 子结点结构为list
} else if (map.get(key) instanceof List) {
List<Object> list = (ArrayList<Object>) map.get(key);
for (int i = 0; i < list.size(); i++) {
mapToXml(e, key, (Map<String, Object>) list.get(i));
}
} else {
String value = map.get(key) == null ? "" : map.get(key)
.toString();
e.addElement(key).addText(value);
// e.addElement(key).addCDATA(value);
}
}
return e.asXML();
}
/**
* 格式化xml
*
* @param xml 未格式化的xml
* @return String 格式化的xml
*/
private static String formatXML(String xml) {
String requestXML = null;
XMLWriter writer = null;
Document document;
try {
SAXReader reader = new SAXReader();
document = reader.read(new StringReader(xml));
if (document != null) {
StringWriter stringWriter = new StringWriter();
// 格式化,层级间的缩进
OutputFormat format = new OutputFormat(" ", true);
// xml声明与内容是否添加空行
format.setNewLineAfterDeclaration(false);
// 是否设置xml声明头部 false:添加
format.setSuppressDeclaration(false);
// 换行
format.setNewlines(true);
writer = new XMLWriter(stringWriter, format);
writer.write(document);
writer.flush();
requestXML = stringWriter.getBuffer().toString();
}
return requestXML;
} catch (Exception e1) {
e1.printStackTrace();
return null;
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* xml文件写入磁盘
*
* @param xml
* @param fileName
* @return
* @throws Exception
*/
public static boolean createXmlFile(String filePath, String fileName, String xml) {
FileWriter fw = null;
if (StringUtils.isEmpty(xml)) {
return false;
}
String localPath = null;
try {
// 生成本地文件
localPath = filePath + File.separator + fileName + ".xml";
// 检查文件路径,文件夹有则写入,没有先创建文件夹再写文件
createParentPath(new File(localPath));
fw = new FileWriter(localPath);
fw.write(xml);
fw.flush();
logger.info("xml文件生成成功,路径:{}", localPath);
} catch (IOException e) {
logger.error("xml文件生成失败,路径:{}", localPath, e);
return false;
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
logger.error("FileWriter资源关闭失败:{}", e.getMessage());
}
}
}
return true;
}
/**
* 递归创建父级文件夹
*
* @param file 完整路径文件名(注:不是文件夹)
*/
public static void createParentPath(File file) {
File parentFile = file.getParentFile();
// 判断文件夹是否已存在,没有则创建
if (null != parentFile && !parentFile.exists()) {
parentFile.mkdirs(); // 创建文件夹
createParentPath(parentFile); // 递归创建父级目录
}
}
}
java处理XML工具类
最新推荐文章于 2024-02-20 09:39:46 发布