⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectfromxmlload.java

📁 一个专门用来快速开发网站的框架
💻 JAVA
字号:
package com.core.init;

/**
 * 解析SelectFromXML.xml,格式:
 <mdgl-config>
         <config name="LOGON_ACTION_NAME" value="wewe"/>
         <config name="PAGE_SIZE" value="3"/>
     </mdgl-config>
 */
import java.util.ArrayList;

import java.io.File;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.core.taglib.SelcetCache;
import com.core.taglib.components.SelectOption;
import java.io.InputStream;

/**
 * 加载MDGLConfig.xml配置文件中的值,赋给Config的变量。
 <?xml version="1.0" encoding="GBK"?>
 <SelectDefineByXML>
        <select name="xb">
                <item value="1" option="男"/>
                <item value="0" option="女"/>
        </select>
        <select name="lb">
                <item value="1" option="部门"/>
                <item value="2" option="岗位"/>
        </select>
 </SelectDefineByXML>

 */
public class SelectFromXMLLoad {
    private static Log log = LogFactory.getLog(SelectFromXMLLoad.class);

    public SelectFromXMLLoad() {
    }

    //在元属开始事件中进行的工作
    public void parserXML(String fileName) {
        try {
            // 从类路径读取配置文件。
            InputStream stream = this.getClass().getResourceAsStream(fileName);
            SAXReader reader = new SAXReader();
            Document doc = reader.read(stream);
            Element root = doc.getRootElement();
            log.info("Root:" + root.getName());

            // 解析select集合
            Element select;
            for (Iterator i = root.elementIterator("select"); i.hasNext(); ) {
                select = (Element) i.next();
                Attribute aName = (Attribute) select.attribute("name");
                String stlectName = aName.getValue();
                log.info("Select:" + aName.getValue());
                // 解析item集合
                Element item;
                ArrayList list = new ArrayList();
                for (Iterator k = select.elementIterator("item"); k.hasNext(); ) {
                    item = (Element) k.next();
                    Attribute aValue = (Attribute) item.attribute("value");
                    Attribute aOption = (Attribute) item.attribute("option");
                    list.add(new SelectOption(aValue.getValue(), aOption.getValue()));
                    log.info("Value:" + aValue.getValue() + "Option" + aOption.getValue());
                }
                // 添加入缓存
                SelcetCache.getInstance().addSelect(stlectName, list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* main method for test: */
    public static void main(String[] args) {
        SelectFromXMLLoad u = new SelectFromXMLLoad();
        u.parserXML("F:\\SelectFromXML.xml");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -