记一次批量修改XML文件节点
package com.sysware.p2m.service.model.core.user; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; import java.io.FileWriter; import java.util.*; import java.util.stream.Collectors; /** * @author SPC-044 * @date 9:23 * @Version 1.0 */ public class Main { public static void main(String[] args) { //要扫描的文件目录 String xmlDirectory = "D:\\GitRepository\\p2m\\service-model\\core\\src\\main\\resources\\config\\model\\datamodeltemplate"; File dir = new File(xmlDirectory); File[] xmlFiles = dir.listFiles((dir1, name) -> name.endsWith(".xml")); //基础模型模板 String basePath ="D:\\GitRepository\\p2m\\service-model\\core\\src\\main\\resources\\AAAAAAAAAAAAutoCAD.xml"; File basePathxml = new File(basePath); Map<String,List<Attribute>> baseMap= new HashMap<>(); // String baseStr ="id," + // "modelTypeId," + // "modelId," + // "name," + // "creatorId," + // "createTime," + // "updaterId," + // "updateTime," + // "statusId," + // "description," + // "rowStatusId," + // "versionName," + // "unitId," + // "rootId," + // "projectPhaseId," + // "discipline," + // "importFlag," + // "templateCode," + // "departmentType," + // "arrayLength," + // "dimension," + // "securityValueId," + // "documentTypeId," + // "integrationItemInfo," + // "dataControlLevelId," + // "valueContent," + // "versionId"; // String[] split = baseStr.split(","); // List<String> list = Arrays.asList(split); try { Document baseDocument = new SAXReader().read(basePathxml); Element rootElement = baseDocument.getRootElement(); List<Element> elements = rootElement.elements(); for (Element element : elements) { List<Attribute> attributes = element.attributes(); baseMap.put(element.attributeValue("name"),attributes); } // 批量添加节点 for (File xmlFile : xmlFiles) { List<String> baseList = new ArrayList<>(); baseList.addAll(baseMap.keySet()); // 读取xml文件 Document document = new SAXReader().read(xmlFile); Element root2 = document.getRootElement(); List<Element> listElement = root2.elements();//所有一级子节点的list //删除多余节点 // for (Element element : listElement) { // String name = element.attributeValue("name"); // if(!list.contains(name)){ // root2.remove(element); // System.out.println(xmlFile.getName()+">>>>>>>>>>>"+name); // // } // } List<String> nameList = listElement.stream().map(e -> e.attributeValue("name")).collect(Collectors.toList()); baseList.removeAll(nameList); if(baseList.isEmpty()) continue; for (String string : baseList) { Element aElement = root2.addElement("field"); List<Attribute> attributes = baseMap.get(string); attributes.stream().forEach(e->{ aElement.addAttribute(e.getName(),e.getValue()); }); System.out.println(xmlFile.getName()+">>>>>>>>>>>"+string); } FileWriter writer = new FileWriter(xmlFile); // 创建一个XMLWriter对象,并设置格式化选项 XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint()); // 设置XMLWriter的输出格式 xmlWriter.setIndentLevel(1); // 将根元素写入输出流中,并根据设置的格式化选项进行格式化 xmlWriter.write(document); xmlWriter.close(); } } catch (Exception e) { throw new RuntimeException(e); } } }