zoukankan      html  css  js  c++  java
  • xml解析例子3

    xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <isOpen>true</isOpen>
        <refreshSpan>100000</refreshSpan>
        <tables>
            <table name="CACHE_TEST" key="ID" />
            <table name="CACHE_TEST1" key="ID1,ID2" />
            <table name="CACHE_TEST2" key="" />
        </tables>
        <test1>
            <table>
                <name value="vip">liweiVIP2</name>
                <age>24</age>
            </table>
            <table>
                <name value="normal">liwei2</name>
                <age>25</age>
            </table>
            <table>
                <name value="normal">liwei20</name>
                <age>25</age>
            </table>
        </test1>
        <test1 num = "second">
            <table>
                <name value="vip">liweiVIP3</name>
                <age>24</age>
            </table>
            <table>
                <name value="normal">liwe3</name>
                <age>25</age>
            </table>
            <table>
                <name value="normal">liwei30</name>
                <age>25</age>
            </table>
        </test1>
        <test3>
            <table>
                <name value="vip">liweiVIP3</name>
                <age>24</age>
            </table>
            <table>
                <name value="normal">liwe3</name>
                <age>25</age>
            </table>
            <table key = "noraml">
                <name value="normal">liwei30</name>
                <age>25</age>
            </table>
        </test3>
    </root>

    java代码

    package jetsennet.cache;
    
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.log4j.Logger;
    import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    
    import jetsennet.sqlclient.DbConfig;
    
    public class CacheConfig
    {
    
        /**
         * 缓存是否开启
         */
        public static boolean isOpen;
        /**
         * 刷新间隔,单位为秒
         */
        public static int refreshSpan;
        /**
         * 需要缓存的表
         */
        public static List<String> tables = new ArrayList<String>();
        /**
         * 表为键,主键为值
         */
        public static Map<String, String> table2key = new HashMap<String, String>();
        /**
         * 日志
         */
        private static final Logger logger = Logger.getLogger(CacheConfig.class);
    
        private Document doc = null;
    
        static
        {
            InputStream input = null;
            try
            {
                input = DbConfig.class.getResourceAsStream("/cache.xml");
                SAXBuilder sax = new SAXBuilder();
                Document doc = sax.build(input);
                Element root = doc.getRootElement();
                Element isOpenEle = root.getChild("isOpen");
                Element refreshSpanEle = root.getChild("refreshSpan");
                Element tablesEle = root.getChild("tables");
                if (isOpenEle != null)
                {
                    String isOpenS = isOpenEle.getTextTrim();
                    isOpen = "true".equalsIgnoreCase(isOpenS);
                    if (isOpen)
                    {
                        if (tablesEle != null)
                        {
                            List<Element> tableEleLst = tablesEle.getChildren("table");
                            if (tableEleLst != null && tableEleLst.size() > 0)
                            {
                                int tableEleCount = tableEleLst.size();
                                for (int i = 0; i < tableEleCount; i++)
                                {
                                    String name = null;
                                    String key = null;
                                    Element tableEle = tableEleLst.get(i);
                                    Attribute nameAttr = tableEle.getAttribute("name");
                                    Attribute keyAttr = tableEle.getAttribute("key");
                                    if (nameAttr != null && keyAttr != null)
                                    {
                                        name = nameAttr.getValue().trim();
                                        key = keyAttr.getValue().trim();
                                    }
                                    if (name != null && key != null)
                                    {
                                        if (!table2key.containsKey(name))
                                        {
                                            table2key.put(name, key);
                                            tables.add(name);
                                        }
                                    }
                                }
                            }
                        }
                    }
    
                    // test________________________________________________________________________________________________________
                    List<Element> test1List = root.getChildren("test1");
                    for (Element e1 : test1List)
                    {
                        List<Element> tableList = e1.getChildren("table");
                        for (Element e2 : tableList)
                        {
                            String nameValue = e2.getChild("name").getAttributeValue("value");
                            if ("normal".equals(nameValue))
                            {
                                System.out.println(e2.getChild("name").getTextTrim());
                            }
                        }
                    }
    
                    if (refreshSpanEle != null)
                    {
                        refreshSpan = Integer.valueOf(refreshSpanEle.getTextTrim());
                    }
                }
                else
                {
                    isOpen = false;
                }
            }
            catch (Exception e)
            {
                isOpen = false;
                logger.error("cache.xml文件出错,不开启缓存。", e);
            }
            finally
            {
                if (input != null)
                {
                    try
                    {
                        input.close();
                    }
                    catch (Exception ex)
                    {
                        logger.error("", ex);
                    }
                    finally
                    {
                        input = null;
                    }
                }
            }
        }
    
        public static boolean containTable(String tableName)
        {
            return table2key.containsKey(tableName);
        }
    
        public static String getKey(String tableName)
        {
            return table2key.get(tableName);
        }
    
        public static void main(String[] args)
        {
            System.out.println(CacheConfig.isOpen);
            System.out.println(CacheConfig.tables);
        }
    
    }
    

      

  • 相关阅读:
    tile38 复制配置
    The Guardian’s Migration from MongoDB to PostgreSQL on Amazon RDS
    tile38 一款开源的geo 数据库
    sqler sql 转rest api 的docker 镜像构建(续)使用源码编译
    sqler sql 转rest api javascript 试用
    sqler sql 转rest api redis 接口使用
    sqler sql 转rest api 的docker image
    sqler sql 转rest api 的工具试用
    apache geode 试用
    benthos v1 的一些新功能
  • 原文地址:https://www.cnblogs.com/liwei45212/p/3064880.html
Copyright © 2011-2022 走看看