{"id":1300,"date":"2021-04-06T14:29:25","date_gmt":"2021-04-06T06:29:25","guid":{"rendered":"http:\/\/diuut.com\/?p=1300"},"modified":"2021-04-06T14:29:42","modified_gmt":"2021-04-06T06:29:42","slug":"java%e4%b8%ad%e6%89%b9%e9%87%8f%e8%a7%a3%e6%9e%90%e5%b9%b6%e7%a7%bb%e9%99%a4xml%e6%a0%bc%e5%bc%8f%e6%96%87%e4%bb%b6%e6%8c%87%e5%ae%9a%e8%8a%82%e7%82%b9","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1300","title":{"rendered":"JAVA\u4e2d\u6279\u91cf\u89e3\u6790\u5e76\u79fb\u9664XML\u683c\u5f0f\u6587\u4ef6\u6307\u5b9a\u8282\u70b9"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42\uff1a<\/p><cite>\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5217\u8868\uff0c\u4e00\u4e2a\u9700\u8981\u4fdd\u7559\u7684\u63a5\u53e3\u5217\u8868\uff0c\u5c06\u6587\u4ef6\u8def\u5f84\u5217\u8868\u4e2d\u6240\u6709\u6587\u4ef6\u79fb\u9664\u6240\u6709\u9664\u4e86\u4fdd\u7559\u63a5\u53e3\u4ee5\u5916\u7684\u63a5\u53e3\u3002<\/cite><\/blockquote>\n\n\n\n<p>\u4e3b\u8981\u6709\u4e24\u4e2a\u9700\u8981\u89e3\u51b3\u7684\u95ee\u9898\uff0c\u4e00\u662f\u7b5b\u9009\u51fa\u6240\u6709\u6587\u4ef6\u4e2d\u54ea\u4e9b\u6570\u636e\u9700\u8981\u79fb\u9664\uff0c\u4e8c\u662f\u5982\u4f55\u79fb\u9664\u3002<\/p>\n\n\n\n<p>\u95ee\u9898\u4e00\u901a\u8fc7arrayList\u7684\u4ea4\u96c6\uff08 .retainAll() \uff09\u5dee\u96c6\uff08 .removeAll() \uff09 \u4ee5\u53ca\u5e76\u96c6\uff08  .removeAll() + .addAll() \uff09\u65b9\u6cd5\uff0c\u6765\u5c06\u9700\u8981\u79fb\u9664\u6216\u8005\u4fdd\u7559\u7684\u63a5\u53e3\u8fdb\u884c\u7b5b\u9009\u51fa\u6765\u3002\u518d\u4ea4\u4ed8\u7ed9\u7b2c\u4e8c\u6b65\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u95ee\u9898\u4e8c\uff0c\u901a\u8fc7org.w3c.dom.* \u4ee5\u53cajavax.xml. \u76f8\u5173\u5305\u4e0b\u7684\u5de5\u5177\u8fdb\u884cxml\u7684\u8bfb\u53d6\u7b5b\u9009\u4ee5\u53ca\u64cd\u4f5c\u5e76\u53c2\u8003\u7f51\u7edc\u4e0a\u7684\u76f8\u5173\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage xyz.diuut;\n\nimport java.io.File;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.nio.file.Files;\nimport java.util.ArrayList;\nimport java.util.List;\nimport org.w3c.dom.*;\nimport javax.xml.parsers.*;\nimport javax.xml.transform.*;\nimport javax.xml.transform.dom.DOMSource;\nimport javax.xml.transform.stream.*;\nimport javax.xml.xpath.*;\n\n\/**\n * @Author Diuut\n * @Date 2020\/4\/2  15:32\n *\/\npublic class BizClearUtil {\n\n    \/**\n     * \u590d\u5236\u4e0b\u9762\u8fd9\u6bb5 \u53e6\u5b58\u6587\u4ef6r.bat   \u5728\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u6267\u884c\uff0c\u83b7\u53d6\u8be5\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u6587\u4ef6\u8def\u5f84\n     * DIR \/S\/B &gt;bizslist.TXT\n     *\/\n\n    public static void main(String&#91;] args) {\n        List&lt;String&gt; urlList = getList(&quot;F:\\\\IDEA-workspace\\\\Test_demo\\\\Test_user\\\\src\\\\main\\\\resources\\\\bizslist.TXT&quot;);\n        \/\/\u4e0a\u9762\u8def\u5f84\u653e\u5165\u6267\u884cr.bat\u540e\u751f\u6210\u7684\u6587\u4ef6\u5730\u5740\u5217\u8868\n        List&lt;String&gt; bizUrlList = new ArrayList&lt;String&gt;();          \/\/biz \u6587\u4ef6\u8def\u5f84\u5217\u8868\n        assert urlList != null;\n        for (String temp : urlList) {\n            if (temp.contains(&quot;.biz&quot;)) {\n                bizUrlList.add(temp);\n            }\n        }\n\/\/        System.out.println(bizUrlList);\n\n        List&lt;String&gt; apiListAndDo = getList(&quot;F:\\\\IDEA-workspace\\\\Test_demo\\\\Test_user\\\\src\\\\main\\\\resources\\\\mbankApi.txt&quot;);\n        \/\/\u4e0a\u9762\u8def\u5f84\u653e\u5165\u751f\u6210\u597d\u7684\u9700\u8981\u4fdd\u5b58\u7684api\n        List&lt;String&gt; apiList = new ArrayList&lt;String&gt;();          \/\/api \u6587\u4ef6\u540d\u5217\u8868  \u4e0d\u5305\u542bdo\n        assert apiListAndDo != null;\n        for (String temp : apiListAndDo) {\n            String replace = temp.replace(&quot;.do&quot;, &quot;&quot;);\n            apiList.add(replace);\n        }\n\/\/         System.out.println(apiList);\n\n        for (String bizUrl : bizUrlList) {              \/\/\u904d\u5386\u8c03\u7528\u6e05\u9664\u4e0d\u9700\u8981\u4fdd\u5b58\u7684api\n            clearBizApi(bizUrl, apiList);\n            clearEmptyBiz(bizUrl);\n        }\n    }\n\n    \/**\n     * \u5220\u9664\u7a7a\u7684Biz\u6587\u4ef6\n     *\n     * @param bizUrl birUrl\u8def\u5f84\n     *\/\n    public static void clearEmptyBiz(String bizUrl) {\n        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\n        Element theBook = null, root = null;\n        try {\n            factory.setIgnoringElementContentWhitespace(true);\n            DocumentBuilder db = factory.newDocumentBuilder();\n            Document xmldoc = (Document) db.parse(new File(bizUrl));\n            root = xmldoc.getDocumentElement();\n            xmldoc.setXmlStandalone(true);\n            \/\/\u6253\u5370\u67e5\u627e\u7684\u8282\u70b9\n            NodeList nodeList = selectNodes(&quot;\/MCITransaction\/operation&quot;, root);\n            List&lt;String&gt; currentIds = new ArrayList&lt;&gt;();\n            for (int i = 0; i &lt; nodeList.getLength(); i++) {\n                String id = nodeList.item(i).getAttributes().getNamedItem(&quot;id&quot;).toString();\n                String substring = id.substring(4, id.length() - 1);\n                currentIds.add(substring);\n            }\n            if (currentIds.size() == 0) {\n                System.out.println(&quot;\u5f53\u524dbiz\u6587\u4ef6\u4e2d\u4e0d\u4e0d\u5305\u542b\u9700\u8981\u4fdd\u7559\u7684operation\uff0c\u6545\u5220\u9664\u8be5\u6587\u4ef6\uff1a&quot; + bizUrl);\n                File file = new File(bizUrl);\n                file.delete();\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/**\n     * \u6e05\u9664\u9664\u6307\u5b9aapi\u5916\u7684\u5176\u4ed6operation\n     *\n     * @param bizUrl      birUrl\u8def\u5f84\n     * @param saveApiList \u9700\u8981\u4fdd\u5b58\u7684api\u5217\u8868\n     *\/\n    public static void clearBizApi(String bizUrl, List&lt;String&gt; saveApiList) {\n        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\n        Element theBook = null, root = null;\n        try {\n            factory.setIgnoringElementContentWhitespace(true);\n            DocumentBuilder db = factory.newDocumentBuilder();\n            Document xmldoc = (Document) db.parse(new File(bizUrl));\n            root = xmldoc.getDocumentElement();\n            xmldoc.setXmlStandalone(true);\n            \/\/\u6253\u5370\u67e5\u627e\u7684\u8282\u70b9\n            NodeList nodeList = selectNodes(&quot;\/MCITransaction\/operation&quot;, root);\n            List&lt;String&gt; currentIds = new ArrayList&lt;&gt;();\n\n            for (int i = 0; i &lt; nodeList.getLength(); i++) {\n                String id = nodeList.item(i).getAttributes().getNamedItem(&quot;id&quot;).toString();\n                String substring = id.substring(4, id.length() - 1);\n                currentIds.add(substring);\n            }\n            System.out.println(bizUrl + &quot; \u4e2d\u6240\u6709\u7684 operationId\uff1a\\n&quot; + currentIds);\n            List&lt;String&gt; allApi = new ArrayList&lt;&gt;(currentIds);\n\n            boolean flag1 = currentIds.retainAll(saveApiList);           \/\/\u5f53\u524dapi\u5217\u8868\u4e0e\u4fdd\u7559api\u5217\u8868\u7684\u4ea4\u96c6\uff0c\u5373\u5b58\u5728\u7684api\u5b58\u5728\u4e8e\u8be5list\u4e2d\n            System.out.println(&quot;flag1:&quot; + flag1);\n            System.out.println(bizUrl + &quot; \u4e2d\u9700\u8981\u4fdd\u5b58 operationId\uff1a\\n&quot; + currentIds);\n\n            boolean flag2 = allApi.removeAll(currentIds);          \/\/\u6240\u6709api\u7684\u5217\u8868\u4e0e\u4fdd\u7559api\u5217\u8868\u7684\u5dee\u96c6\uff0c\u5373\u9700\u8981\u79fb\u9664\u7684api\u5217\u8868\n            System.out.println(&quot;flag2:&quot; + flag2);\n            System.out.println(bizUrl + &quot;\u4e2d\u9700\u8981\u79fb\u9664\u7684 operationId\uff1a\\n&quot; + allApi);\n\n            if (allApi.size() == 0) {\n                System.out.println(bizUrl + &quot; \u4e2d\u6ca1\u6709\u9700\u8981\u79fb\u9664\u7684 operationId&quot;);\n                return;\n            }\n\n            for (String currentId : allApi) {\n                theBook = (Element) selectSingleNode(&quot;\/MCITransaction\/operation&#91;@id='&quot; + currentId + &quot;']&quot;, root);\n\/\/            \u5220\u9664\u8282\u70b9\n                theBook.getParentNode().removeChild(theBook);\n\/\/            output(xmldoc);\n                saveXml(bizUrl, xmldoc);\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/**\n     * \u6839\u636e\u8def\u5f84\u6587\u4ef6\u751f\u6210\u5217\u8868\n     *\n     * @param path \u5730\u5740\n     * @return\n     *\/\n    public static List&lt;String&gt; getList(String path) {\n        File listFile = new File(path);\n        List&lt;String&gt; urlList = new ArrayList&lt;String&gt;();\n        if (listFile.exists()) {\n            try {\n                urlList = Files.readAllLines(listFile.toPath());\n            } catch (IOException ex) {\n                ex.printStackTrace();\n            }\n            if (urlList.isEmpty()) {\n                System.out.println(&quot;\u8def\u5f84\u6587\u4ef6\u751f\u6210\u4e3a\u7a7a&quot;);\n                return null;\n            }\n        }\n        return urlList;\n    }\n\n\n    \/**\n     * \u5c06node\u7684XML\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u63a7\u5236\u53f0\n     *\n     * @param node\n     *\/\n    public static void output(Node node) {\n        TransformerFactory transFactory = TransformerFactory.newInstance();\n        try {\n            Transformer transformer = transFactory.newTransformer();\n            transformer.setOutputProperty(&quot;encoding&quot;, &quot;gb2312&quot;);\n            transformer.setOutputProperty(&quot;indent&quot;, &quot;yes&quot;);\n            DOMSource source = new DOMSource();\n            source.setNode(node);\n            StreamResult result = new StreamResult();\n            result.setOutputStream(System.out);\n            transformer.transform(source, result);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/**\n     * \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u8282\u70b9\n     *\n     * @param express\n     * @param source\n     * @return\n     *\/\n    public static Node selectSingleNode(String express, Object source) {\n        Node result = null;\n        XPathFactory xpathFactory = XPathFactory.newInstance();\n        XPath xpath = xpathFactory.newXPath();\n        try {\n            result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);\n        } catch (XPathExpressionException e) {\n            e.printStackTrace();\n        }\n        return result;\n    }\n\n    \/**\n     * \u67e5\u627e\u8282\u70b9\uff0c\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9\u96c6\u3002\n     *\n     * @param express\n     * @param source\n     * @return\n     *\/\n    public static NodeList selectNodes(String express, Object source) {\n        NodeList result = null;\n        XPathFactory xpathFactory = XPathFactory.newInstance();\n        XPath xpath = xpathFactory.newXPath();\n        try {\n            result = (NodeList) xpath.evaluate(express, source,\n                    XPathConstants.NODESET);\n        } catch (XPathExpressionException e) {\n            e.printStackTrace();\n        }\n        return result;\n    }\n\n    \/**\n     * \u5c06Document\u8f93\u51fa\u5230\u6587\u4ef6\n     *\n     * @param fileName\n     * @param doc\n     *\/\n    public static void saveXml(String fileName, Document doc) {\n        TransformerFactory transFactory = TransformerFactory.newInstance();\n        try {\n            Transformer transformer = transFactory.newTransformer();\n            transformer.setOutputProperty(&quot;indent&quot;, &quot;yes&quot;);     \/\/standalone=&quot;no&quot; \u914d\u7f6exml\u5934\u6587\u4ef6\u5c5e\u6027\n            DOMSource source = new DOMSource();\n            source.setNode(doc);\n            StreamResult result = new StreamResult();\n            result.setOutputStream(new FileOutputStream(fileName));\n            transformer.transform(source, result);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote\"><p>\u53c2\u8003\u535a\u5ba2<\/p><cite><a href=\"https:\/\/blog.csdn.net\/u013183865\/article\/details\/32165289\">https:\/\/blog.csdn.net\/u013183865\/article\/details\/32165289<\/a><\/cite><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a \u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5217\u8868\uff0c\u4e00\u4e2a\u9700\u8981\u4fdd\u7559\u7684\u63a5\u53e3\u5217\u8868\uff0c\u5c06\u6587\u4ef6\u8def\u5f84\u5217\u8868\u4e2d\u6240\u6709\u6587\u4ef6\u79fb\u9664\u6240\u6709\u9664\u4e86\u4fdd\u7559\u63a5\u53e3\u4ee5\u5916\u7684<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1300\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">JAVA\u4e2d\u6279\u91cf\u89e3\u6790\u5e76\u79fb\u9664XML\u683c\u5f0f\u6587\u4ef6\u6307\u5b9a\u8282\u70b9<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[37,38],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1300"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1300"}],"version-history":[{"count":2,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1300\/revisions"}],"predecessor-version":[{"id":1302,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1300\/revisions\/1302"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/657"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}