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

2023-05-16

一、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;

/**
 * @author shtq.001
 */
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<Map<String, Object>> resultList = new ArrayList<>();
            for (int i = 0; i < files.length; i++) {
                if (!files[i].exists()) {
                    System.out.println("xml文件不存在,请确认!");
                } else {
                    List<Map<String, Object>> tempList = parseXML(files[i]);
                    if (tempList != null && tempList.size() > 0) {
                        resultList.addAll(tempList);
                    }
                }
            }
            System.err.println(resultList.size());
            for (Map<String, Object> map : resultList) {
                System.err.println(map);
            }
        }
    }

    /**
     * 解析xml文件
     *
     * @param file
     * @return
     */
    public static List<Map<String, Object>> 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<Map<String, Object>> 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<Map<String, Object>> parseElement(Element element, List<Map<String, Object>> resultList){
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Map<String, Object> 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)}

 四、说明

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐