java通过org.w3c.dom解析xml文件

阅读更多

要点:

 

        // 属性值获取  String Element.getAttribute(name);
        //标签值通过父元素对象获取  NodeList  parentElement.getElementsByTagName(name);
        //标签中间的值获取  String Element.getTextContent()

 

    private void loadSdataConfig() throws RuntimeException {
        // 属性值获取  String Element.getAttribute(name);
        //标签值通过父元素对象获取  NodeList  parentElement.getElementsByTagName(name);
        //标签中间的值获取  String Element.getTextContent()
        try {
            String sdataPath = SdataConfig.class.getResource("/sdata.xml").toURI().getPath();
            if (sdataPath == null) {
                throw new RuntimeException("sdata.xml path is null");
            }

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = null;
            Document document = null;
            try {
                builder = factory.newDocumentBuilder();
                document = builder.parse(sdataPath);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Element element = document.getDocumentElement();

            NodeList sdataPortList = element.getElementsByTagName("sdata-port");
            if (sdataPortList != null && sdataPortList.getLength() > 0) {
                sdataPort = Integer.parseInt(sdataPortList.item(0).getTextContent());
            }
            NodeList dataBrowserPortList = element.getElementsByTagName("data-browser-port");
            if (dataBrowserPortList != null && dataBrowserPortList.getLength() > 0) {
                dataBrowserPort = Integer.parseInt(dataBrowserPortList.item(0).getTextContent());
            }
            NodeList commandPortList = element.getElementsByTagName("command-port");
            if (commandPortList != null && commandPortList.getLength() > 0) {
                commandPort = Integer.parseInt(commandPortList.item(0).getTextContent());
            }
            LOG.info("sdata-port:{} , data-browser-port:{} , command-port:{} ", sdataPort, dataBrowserPort, commandPort);

            NodeList marketInitJobsList = element.getElementsByTagName("marketInitPushJobs");
            if (marketInitJobsList != null && marketInitJobsList.getLength() > 0) {
                Element marketInitPushJob = (Element) marketInitJobsList.item(0);
                NodeList marketInitPushJobList = marketInitPushJob.getElementsByTagName("marketInitPushJob");
                for (int i = 0; i < marketInitPushJobList.getLength(); i++) {
                    Element ele = (Element) marketInitPushJobList.item(i);
                    Job job = new Job();
                    String name = ele.getAttribute("name");
                    job.setName(name);
                    String cron = ele.getAttribute("cron");
                    if (cron != null && cron.trim().length() > 0) {
                        job.setCrons(Arrays.asList(cron.split(";")));
                    }
                    String clazz = ele.getAttribute("class");
                    job.setJobClass(clazz);
                    marketInitPushJobs.add(job);
                }
            }
            LOG.info("marketInitPushJobs msgs:{}", marketInitPushJobs);

            NodeList verdatasList = element.getElementsByTagName("verdatas");
            if (verdatasList != null && verdatasList.getLength() > 0) {
                Element verdatasElement = (Element) verdatasList.item(0);
                NodeList verdataList = verdatasElement.getElementsByTagName("verdata");
                if (verdataList != null && verdataList.getLength() > 0) {
                    for (int i = 0; i < verdataList.getLength(); i++) {
                        Element verdataElement = (Element) verdataList.item(i);
                        Verdata verdata = new Verdata();
                        String verdataName = verdataElement.getAttribute("name");
                        verdata.setName(verdataName);
                        String adapt = verdataElement.getAttribute("isAdapt");
                        if (adapt != null && adapt.trim().length() > 0) {
                            verdata.setAdapt(Boolean.parseBoolean(adapt));
                        }
                        NodeList processorList = verdataElement.getElementsByTagName("processor");
                        if (processorList != null && processorList.getLength() > 0) {
                            for (int j = 0; j < processorList.getLength(); j++) {
                                Element processorElement = (Element) processorList.item(j);
                                Processor processor = new Processor();
                                processor.setId(processorElement.getAttribute("id"));
                                processor.setName(processorElement.getAttribute("name"));
                                processor.setDataId(Integer.parseInt(processorElement.getAttribute("dataId")));
                                processor.setCodecClass(processorElement.getAttribute("codecClass"));
                                processor.setFactoryClass(processorElement.getAttribute("factoryClass"));
                                String isInit = processorElement.getAttribute("isInit");
                                if (isInit != null && isInit.trim().length() > 0) {
                                    processor.setInit(Boolean.parseBoolean(isInit));
                                }
                                List processors = verdata.getProcessors();
                                if (processors == null) {
                                    processors = new ArrayList<>();
                                    verdata.setProcessors(processors);
                                }
                                processors.add(processor);
                            }
                        }
                        verdatas.add(verdata);
                    }
                }
            }
            LOG.info("verdatas msg:{}", verdatas);

            NodeList jobsList = element.getElementsByTagName("jobs");
            if (jobsList != null && jobsList.getLength() > 0) {
                Element jobsElement = (Element) jobsList.item(0);
                NodeList jobList = jobsElement.getElementsByTagName("job");
                if (jobList != null && jobList.getLength() > 0) {
                    for (int i = 0; i < jobList.getLength(); i++) {
                        Element jobElement = (Element) jobList.item(i);
                        Job job = new Job();
                        job.setName(jobElement.getAttribute("name"));
                        String cron = jobElement.getAttribute("cron");
                        if (cron != null && cron.trim().length() > 0) {
                            job.setCrons(Arrays.asList(cron.split(";")));
                        }
                        job.setJobClass(jobElement.getAttribute("jobClass"));
                        jobs.add(job);
                    }
                }
            }
            LOG.info("jobs msg: {}", jobs);


        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

 

sdata.xml文件

 

 



    1204
    1205
    1206

    
        
        
    

    
    

        
            
            
        

        
            
            
        

        
            
            
        


        
            
            
        

        
            
        


        
            
        


        
            
        

        
            
        




        
            
            
        
    



 

 

maven依赖:

        
            dom4j
            dom4j
            1.6.1
        

 

 

你可能感兴趣的:(java解析xml文件,dom4j解析xml文件)