欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java通过org.w3c.dom解析xml文件 博客分类: xml java解析xml文件dom4j解析xml文件 

程序员文章站 2024-03-20 16:00:04
...

要点:

 

        // 属性值获取  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<Processor> 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文件

 

 

<?xml version="1.0" encoding='UTF-8'?>
<sdata-config>
    <sdata-port desc="">1204</sdata-port>
    <data-browser-port desc="">1205</data-browser-port>
    <command-port desc="">1206</command-port>

    <marketInitPushJobs>
        <marketInitPushJob name="SHSZ" cron="0 52,55 8 * * ?;0 1,15,30 9 * * ?"
                           class="com.eastmoney.sdata.job.MarketInitPushJob" desc="沪深市场早盘推数据Job"/>
        <marketInitPushJob name="StaticBlock" cron="0 0 1 * * ?"
                           class="com.eastmoney.sdata.db.staticblock.StaticBlockSecurityIdJob" desc="静态指数内码集合Job"/>
    </marketInitPushJobs>

    <!-- -->
    <verdatas>

        <verdata name="shszfactor" isAdapt="true">
            <processor id="shszfactor-10229" name="shszfactor" dataId="10229"   factoryClass="com.eastmoney.sdata.verdata.impl.factor.SHSZFactorFactory" codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
            <processor id="shszfactor-10129" name="shszfactor" dataId="10129" isInit="true" factoryClass="com.eastmoney.sdata.verdata.impl.factor.SHSZFactorFactory" codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
        </verdata>

        <verdata name="hkfactor" isAdapt="true">
            <processor id="hkfactor-10232" name="hkfactor" dataId="10232"   factoryClass="com.eastmoney.sdata.verdata.impl.factor.HKFactorFactory" codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
            <processor id="hkfactor-10132" name="hkfactor" dataId="10132"  isInit="true" factoryClass="com.eastmoney.sdata.verdata.impl.factor.HKFactorFactory" codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
        </verdata>

        <verdata name="ussfactor" isAdapt="true">
            <processor id="ussfactor-10233" name="ussfactor" dataId="10233" isInit="false" factoryClass="com.eastmoney.sdata.verdata.impl.factor.USSFactorFactory"
                       codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
            <processor id="ussfactor-10133" name="ussfactor" dataId="10133" isInit="false" factoryClass="com.eastmoney.sdata.verdata.impl.factor.USSFactorFactory"
                       codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
        </verdata>


        <verdata name="sanbanfactor" isAdapt="true">
            <processor id="sanbanfactor-10234" name="" dataId="10234" isInit="false" factoryClass="com.eastmoney.sdata.verdata.impl.factor.SanbanFactorFactory"
                       codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
            <processor id="sanbanfactor-10134" name="" dataId="10134" isInit="true" factoryClass="com.eastmoney.sdata.verdata.impl.factor.SanbanFactorFactory"
                       codecClass="com.eastmoney.sdata.verdata.impl.factor.FactorEncode" desc=""/>
        </verdata>

        <verdata name="finance" isAdapt="false">
            <processor id="finance-10030" name="finance" dataId="10030" isInit="true" factoryClass="com.eastmoney.sdata.verdata.impl.finance.FinanceFacotry"
                       codecClass="com.eastmoney.sdata.verdata.impl.finance.FinanceEncode" desc=""/>
        </verdata>


        <verdata name="price" isAdapt="false">
            <processor id="price-10031" name="price" dataId="10031" isInit="true" factoryClass="com.eastmoney.sdata.verdata.impl.price.PriceFactory" codecClass="com.eastmoney.sdata.verdata.impl.price.PriceEncode" desc=""/>
        </verdata>


        <verdata name="date" isAdapt="false">
            <processor id="date-10035" name="date"  dataId="10035" isInit="true" factoryClass="com.eastmoney.sdata.verdata.impl.date.DateFactory" codecClass="com.eastmoney.sdata.verdata.impl.date.DateEncode" desc=""/>
        </verdata>

        <verdata name="optionbond" isAdapt="false">
            <processor id="optionbond-10040" name="optionbond"  dataId="10040" isInit="true" factoryClass="com.eastmoney.sdata.verdata.impl.optionbond.OptionBondDataFactory" codecClass="com.eastmoney.sdata.verdata.impl.optionbond.OptionBondEncode" desc=""/>
        </verdata>




        <jobs>
            <job name="SHSZFQCalJob" cron="0 0 7 * * ?" jobClass="com.eastmoney.sdata.verdata.job.SHSZFQCalJob" desc=""/>
            <job name="SHSZSanbanFactorUpdateJob" cron="0 50 8 * * ?;0 05,30 9 * * ?" jobClass="com.eastmoney.sdata.verdata.job.SHSZSanbanFactorUpdateJob" desc=""/>
        </jobs>
    </verdatas>


</sdata-config>

 

 

maven依赖:

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>