• 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);
        }
    
    }
    

      

  • 相关阅读:
    2017第5周五随想录~改变活法
    2017第5周四
    《高绩效教练》笔记
    2017第5周二杭州印象
    2017第5周一
    代码自动生成工具_java版
    20个代码生成框架
    通过数据库表自动生成POJO(JavaBean)对象
    java工具类–自动将数据库表生成javabean
    Nginx+Tomcat+Memcached负载均衡集群服务搭建
  • 原文地址:https://www.cnblogs.com/liwei45212/p/3064880.html
Copyright © 2020-2023  润新知