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);
}
}
浙公网安备 33010602011771号