使用Document解析xml格式的文件(以P3C扫描结果为例)

使用Document解析xml格式的文件(以P3C扫描结果为例),第1张

使用Document解析xml格式的文件(以P3C扫描结果为例) 一、xml文件格式(以P3C扫描结果为例)

二、示例代码
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.documentBuilder;
import javax.xml.parsers.documentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class XmlTest2 {
    public static void main(String[] args) {
        String directory = "F:\p3c";
        File file = new File(directory);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            List> resultList = new ArrayList<>();
            for (int i = 0; i < files.length; i++) {
                if (!files[i].exists()) {
                    System.out.println("xml文件不存在,请确认!");
                } else {
                    List> tempList = parseXML(files[i]);
                    if (tempList != null && tempList.size() > 0) {
                        resultList.addAll(tempList);
                    }
                }
            }
            System.err.println(resultList.size());
            for (Map map : resultList) {
                System.err.println(map);
            }
        }
    }

    
    public static List> parseXML(File file) {
        // 初始化一个XML解析工厂
        documentBuilderFactory factory = documentBuilderFactory.newInstance();
        // 创建一个documentBuilder实例
        documentBuilder builder = null;
        try {
            builder = factory.newdocumentBuilder();
            // 创建一个解析XML的document实例
            document document = builder.parse(file);
            // 获取根节点
            Element root = document.getdocumentElement();
            List> resultList = new ArrayList<>();
            resultList = parseElement(root, resultList);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return resultList ;
    }

    public static List> parseElement(Element element, List> resultList){
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Map map = new HashMap<>(16);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if("problem".equals(node.getNodeName())){
                    NodeList nodeList1 = node.getChildNodes();
                    for (int j = 0; j < nodeList1.getLength(); j++) {
                        Node childNode = nodeList1.item(j);
                        if("file".equals(childNode.getNodeName())){
                            map.put("file", childNode.getTextContent());
                        } else if("line".equals(childNode.getNodeName())){
                            map.put("line", childNode.getTextContent());
                        } else if("problem_class".equals(childNode.getNodeName())){
                            map.put("problem", childNode.getTextContent());
                            NamedNodeMap namedNodeMap =  childNode.getAttributes();
                            for (int k = 0; k < namedNodeMap.getLength(); k++) {
                                Attr attr = (Attr) namedNodeMap.item(k);
                                if("severity".equals(attr.getName())){
                                    map.put("severity", attr.getValue());
                                }
                            }
                        } else if("description".equals(childNode.getNodeName())){
                            map.put("suggestion", childNode.getTextContent());
                        } else if("entry_point".equals(childNode.getNodeName())){
                            NamedNodeMap namedNodeMap =  childNode.getAttributes();
                            for (int m = 0; m < namedNodeMap.getLength(); m++) {
                                Attr attr = (Attr) namedNodeMap.item(m);
                                if("FQNAME".equals(attr.getName())){
                                    map.put("functionName", attr.getValue());
                                }
                            }
                        }
                    }
                    resultList.add(map);
                }
            }
        }
        return resultList;
    }
}
三、运行结果
{severity=BLOCKER, file=file://$PROJECT_DIR$/src/main/java/basicstudy/thread/ExecutorsTest.java, problem=线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。, functionName=basicstudy.thread.ExecutorsTest void newSingleThreadTest(), line=26, suggestion=手动创建线程池,效果会更好哦。 (line 26)}
{severity=BLOCKER, file=file://$PROJECT_DIR$/src/main/java/basicstudy/thread/ExecutorsTest.java, problem=线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。, functionName=basicstudy.thread.ExecutorsTest void newCachedThreadPoolTest(), line=75, suggestion=手动创建线程池,效果会更好哦。 (line 75)}
{severity=BLOCKER, file=file://$PROJECT_DIR$/src/main/java/basicstudy/thread/ExecutorsTest.java, problem=线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。, functionName=basicstudy.thread.ExecutorsTest void newFixedThreadPoolTest(), line=49, suggestion=手动创建线程池,效果会更好哦。 (line 49)}

 四、说明

     代码写的简单粗暴,没有经过优化,大家如有意见,欢迎评论,谢谢。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5703640.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存