Site Overlay

JAVA中批量解析并移除XML格式文件指定节点

需求:

两个列表,一个文件路径列表,一个需要保留的接口列表,将文件路径列表中所有文件移除所有除了保留接口以外的接口。

主要有两个需要解决的问题,一是筛选出所有文件中哪些数据需要移除,二是如何移除。

问题一通过arrayList的交集( .retainAll() )差集( .removeAll() ) 以及并集( .removeAll() + .addAll() )方法,来将需要移除或者保留的接口进行筛选出来。再交付给第二步进行操作。

问题二,通过org.w3c.dom.* 以及javax.xml. 相关包下的工具进行xml的读取筛选以及操作并参考网络上的相关代码。

实现代码:

package xyz.diuut;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.*;
import javax.xml.xpath.*;

/**
 * @Author Diuut
 * @Date 2020/4/2  15:32
 */
public class BizClearUtil {

    /**
     * 复制下面这段 另存文件r.bat   在指定文件夹中执行,获取该文件夹下所有文件路径
     * DIR /S/B >bizslist.TXT
     */

    public static void main(String[] args) {
        List<String> urlList = getList("F:\\IDEA-workspace\\Test_demo\\Test_user\\src\\main\\resources\\bizslist.TXT");
        //上面路径放入执行r.bat后生成的文件地址列表
        List<String> bizUrlList = new ArrayList<String>();          //biz 文件路径列表
        assert urlList != null;
        for (String temp : urlList) {
            if (temp.contains(".biz")) {
                bizUrlList.add(temp);
            }
        }
//        System.out.println(bizUrlList);

        List<String> apiListAndDo = getList("F:\\IDEA-workspace\\Test_demo\\Test_user\\src\\main\\resources\\mbankApi.txt");
        //上面路径放入生成好的需要保存的api
        List<String> apiList = new ArrayList<String>();          //api 文件名列表  不包含do
        assert apiListAndDo != null;
        for (String temp : apiListAndDo) {
            String replace = temp.replace(".do", "");
            apiList.add(replace);
        }
//         System.out.println(apiList);

        for (String bizUrl : bizUrlList) {              //遍历调用清除不需要保存的api
            clearBizApi(bizUrl, apiList);
            clearEmptyBiz(bizUrl);
        }
    }

    /**
     * 删除空的Biz文件
     *
     * @param bizUrl birUrl路径
     */
    public static void clearEmptyBiz(String bizUrl) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Element theBook = null, root = null;
        try {
            factory.setIgnoringElementContentWhitespace(true);
            DocumentBuilder db = factory.newDocumentBuilder();
            Document xmldoc = (Document) db.parse(new File(bizUrl));
            root = xmldoc.getDocumentElement();
            xmldoc.setXmlStandalone(true);
            //打印查找的节点
            NodeList nodeList = selectNodes("/MCITransaction/operation", root);
            List<String> currentIds = new ArrayList<>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                String id = nodeList.item(i).getAttributes().getNamedItem("id").toString();
                String substring = id.substring(4, id.length() - 1);
                currentIds.add(substring);
            }
            if (currentIds.size() == 0) {
                System.out.println("当前biz文件中不不包含需要保留的operation,故删除该文件:" + bizUrl);
                File file = new File(bizUrl);
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除除指定api外的其他operation
     *
     * @param bizUrl      birUrl路径
     * @param saveApiList 需要保存的api列表
     */
    public static void clearBizApi(String bizUrl, List<String> saveApiList) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Element theBook = null, root = null;
        try {
            factory.setIgnoringElementContentWhitespace(true);
            DocumentBuilder db = factory.newDocumentBuilder();
            Document xmldoc = (Document) db.parse(new File(bizUrl));
            root = xmldoc.getDocumentElement();
            xmldoc.setXmlStandalone(true);
            //打印查找的节点
            NodeList nodeList = selectNodes("/MCITransaction/operation", root);
            List<String> currentIds = new ArrayList<>();

            for (int i = 0; i < nodeList.getLength(); i++) {
                String id = nodeList.item(i).getAttributes().getNamedItem("id").toString();
                String substring = id.substring(4, id.length() - 1);
                currentIds.add(substring);
            }
            System.out.println(bizUrl + " 中所有的 operationId:\n" + currentIds);
            List<String> allApi = new ArrayList<>(currentIds);

            boolean flag1 = currentIds.retainAll(saveApiList);           //当前api列表与保留api列表的交集,即存在的api存在于该list中
            System.out.println("flag1:" + flag1);
            System.out.println(bizUrl + " 中需要保存 operationId:\n" + currentIds);

            boolean flag2 = allApi.removeAll(currentIds);          //所有api的列表与保留api列表的差集,即需要移除的api列表
            System.out.println("flag2:" + flag2);
            System.out.println(bizUrl + "中需要移除的 operationId:\n" + allApi);

            if (allApi.size() == 0) {
                System.out.println(bizUrl + " 中没有需要移除的 operationId");
                return;
            }

            for (String currentId : allApi) {
                theBook = (Element) selectSingleNode("/MCITransaction/operation[@id='" + currentId + "']", root);
//            删除节点
                theBook.getParentNode().removeChild(theBook);
//            output(xmldoc);
                saveXml(bizUrl, xmldoc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据路径文件生成列表
     *
     * @param path 地址
     * @return
     */
    public static List<String> getList(String path) {
        File listFile = new File(path);
        List<String> urlList = new ArrayList<String>();
        if (listFile.exists()) {
            try {
                urlList = Files.readAllLines(listFile.toPath());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (urlList.isEmpty()) {
                System.out.println("路径文件生成为空");
                return null;
            }
        }
        return urlList;
    }


    /**
     * 将node的XML字符串输出到控制台
     *
     * @param node
     */
    public static void output(Node node) {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty("encoding", "gb2312");
            transformer.setOutputProperty("indent", "yes");
            DOMSource source = new DOMSource();
            source.setNode(node);
            StreamResult result = new StreamResult();
            result.setOutputStream(System.out);
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查找节点,并返回第一个符合条件节点
     *
     * @param express
     * @param source
     * @return
     */
    public static Node selectSingleNode(String express, Object source) {
        Node result = null;
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        try {
            result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查找节点,返回符合条件的节点集。
     *
     * @param express
     * @param source
     * @return
     */
    public static NodeList selectNodes(String express, Object source) {
        NodeList result = null;
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        try {
            result = (NodeList) xpath.evaluate(express, source,
                    XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将Document输出到文件
     *
     * @param fileName
     * @param doc
     */
    public static void saveXml(String fileName, Document doc) {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty("indent", "yes");     //standalone="no" 配置xml头文件属性
            DOMSource source = new DOMSource();
            source.setNode(doc);
            StreamResult result = new StreamResult();
            result.setOutputStream(new FileOutputStream(fileName));
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

参考博客

https://blog.csdn.net/u013183865/article/details/32165289

发表回复

您的电子邮箱地址不会被公开。

A beliving heart is your magic My heart
欢迎来到Diuut的个人博客,这里是我的一些零零碎碎的知识汇总,希望有能帮到你的内容。 | 蜀ICP备2021011635号-1 | Copyright © 2024 Diuut. All Rights Reserved.