📄 xmlconfig.java
字号:
return null;
}
} catch(Exception ex){
return null;
}
}
/**
* 在指定XPath下构建指定PairList中各项值的多个子节点
* @param path String
* @param pairList PairList
*/
public void setPairList(String path, PairList pairList){
Node node = findOrCreateNode(path);
if(node != null && node instanceof Element){
Element ele = (Element)node;
for(int i = 0; i < pairList.size(); i++){
PairEntry entry = pairList.get(i);
ele.addElement(entry.name).setText(entry.value);
}
}
}
/**
* 获取指定xpath下的子节点构成的值列表
* @param path String
* @return List
*/
public List getValueList(String path){
return getValueList(path, null);
}
/**
* 获取指定xpath下的子节点中指定名称节点构成的值列表
* @param path String
* @param name Stirng 为空则匹配所有节点
* @return List
*/
public List getValueList(String path, String name){
try{
Object obj = doc.selectSingleNode(path);
if(obj != null && obj instanceof Element){
return XMLUtils.getChildToList((Element)obj, name);
} else{
return null;
}
} catch(Exception ex){
return null;
}
}
/**
* 在指定XPath下构建指定names中各项值的多个子节点, 并设置相应值
* @param path String
* @param names String[] 名称列表
* @param list List 值列表
*/
public void setValueList(String path, String[] names, List list){
Node node = findOrCreateNode(path);
if(node != null && node instanceof Element){
Element ele = (Element)node;
Object obj = null;
String value = "";
for(int i = 0; i < names.length; i++){
obj = (i < list.size() ? list.get(i) : null);
value = (obj == null ? "" : obj.toString());
ele.addElement(names[i]).setText(value);
}
}
}
/**
* 在指定XPath下构建指定names中各项值的多个子节点, 并设置相应值
* @param path String
* @param nameList List
* @param valueList List
*/
public void setValueList(String path, List nameList, List valueList){
String[] names = new String[nameList.size()];
nameList.toArray(names);
setValueList(path, names, valueList);
}
/**
* 在指定XPath下构建指定list中各项值的多个子节点
* @param path String
* @param name String 节点名称
* @param list List 值列表
*/
public void setValueList(String path, String name, List list){
Node node = findOrCreateNode(path);
if(node != null && node instanceof Element){
Element ele = (Element)node;
Object obj = null;
String value = "";
for(int i = 0; i < list.size(); i++){
obj = list.get(i);
value = (obj == null ? "" : obj.toString());
ele.addElement(name).setText(value);
}
}
}
/**
* 获取指定xpath下的子节点构成的Map
* @param path String
* @return List
*/
public Map getValueMap(String path){
try{
Object obj = doc.selectSingleNode(path);
if(obj != null && obj instanceof Element){
return XMLUtils.getRecursiveMap((Element)obj);
} else{
return null;
}
} catch(Exception ex){
return null;
}
}
/**
* 在指定XPath下构建指定Map中各项名值对构成的多个子节点
* @param path String
* @param map Map
*/
public void setValueMap(String path, Map map){
Node node = findOrCreateNode(path);
if(node != null && node instanceof Element){
Element ele = (Element)node;
Map.Entry entry = null;
Object keyobj = null, valobj = null;
String key = "", value = "";
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
entry = (Map.Entry)it.next();
keyobj = entry.getKey();
if(keyobj != null){
key = keyobj.toString().trim();
if(key.length() > 0){
valobj = entry.getValue();
value = (valobj == null ? "" : valobj.toString());
ele.addElement(key).setText(value);
}
}
}
}
}
/**
* 获取指定节点下的所有子节点列表
* @param path String
* @return List
*/
public List getSubKeyList(String path){
try{
Object obj = doc.selectSingleNode(path);
if(obj != null && obj instanceof Element){
return XMLUtils.getChildNodeNameList((Element)obj);
} else{
return null;
}
} catch(Exception ex){
return null;
}
}
/**
* 获取配置属性的int型值
* @param path String
* @param defValue int
* @return int
*/
public int getInt(String path, int defValue){
try{
String val = getString(path, null);
return Integer.parseInt(val);
} catch(Exception ex){
return defValue;
}
}
/**
* 获取配置属性的long型值
* @param path String
* @param defValue long
* @return long
*/
public long getLong(String path, long defValue){
try{
String val = getString(path, null);
return Long.parseLong(val);
} catch(Exception ex){
return defValue;
}
}
/**
* 获取配置属性的float型值
* @param path String
* @param defValue float
* @return float
*/
public float getFloat(String path, float defValue){
try{
String val = getString(path, null);
return Float.parseFloat(val);
} catch(Exception ex){
return defValue;
}
}
/**
* 获取配置属性的double型值
* @param path String
* @param defValue double
* @return double
*/
public double getDouble(String path, double defValue){
try{
String val = getString(path, null);
return Double.parseDouble(val);
} catch(Exception ex){
return defValue;
}
}
/**
* 获取配置属性的boolean型值
* @param path String
* @param defValue boolean
* @return boolean
*/
public boolean getBoolean(String path, boolean defValue){
String text = getString(path, null);
if(text == null)
return defValue;
else
return(("true".equalsIgnoreCase(text))
|| ("yes".equalsIgnoreCase(text))
|| ("enable".equalsIgnoreCase(text))
|| ("on".equalsIgnoreCase(text))
|| ("1".equalsIgnoreCase(text)));
}
/**
* 从资源文件中得到文件的输入流
* @param resource 资源文件的名称,这个文件应该存在classpath下
* @return 文件的输入流
* @throws IOException 如果读取文件错误,则抛出该异常。
*/
public InputStream getResourceAsStream(String resource) throws Exception{
InputStream in = XMLConfig.class.getClassLoader().getResourceAsStream(resource);
if(in == null){
in = ClassLoader.getSystemResourceAsStream(resource);
}
if(in == null){
in = new FileInputStream(resource);
}
if(in == null)
throw new IOException("无法读取资源文件: " + resource);
return in;
}
public String toXMLString(){
return XMLUtils.toXMLString(doc, "GB2312");
}
public String toXMLString(String encoding){
return XMLUtils.toXMLString(doc, encoding);
}
/**
* 查找指定路径的结点,不存在时构建它
* @param path String
* @return Node
*/
private Node findOrCreateNode(String path){
Node node = null;
try{
doc.selectSingleNode(path);
} catch(Exception ex){
}
if(node == null)
node = DocumentHelper.makeElement(doc, path);
return node;
}
//=========== test ====================================
public static void main(String[] args){
XMLConfig cfg = new XMLConfig();
if(cfg.loadConfig("config.xml")){
Map m = (cfg.getValueMap("/config/gis/maps"));
System.out.println(m);
}
System.out.println(cfg.getAttribute("/config/system/version","type"));
String path = "/cfg/system/test/lucas/zheng";
cfg.initConfig();
cfg.setString(path, ""+System.currentTimeMillis());
cfg.setStrings(path, "tt", new String[]{"1","2","3"});
cfg.setStrings(path, new String[]{"AA","BB","CC","DD","EE"}, new String[]{"A","B","C"});
cfg.setStrings(path, new String[]{"I","II"}, new String[]{"I","II","III"});
List list = new ArrayList();
for(int i = 0; i < 10; i++){
list.add("li_" + i);
}
cfg.setStrings(path, list, new String[]{"L_I","L_II","L_III"});
Map map = new HashMap();
map.put(null,"1");
map.put("", "2");
map.put("3", null);
map.put("4", "");
map.put("5", "m5");
cfg.setValueMap(path, map);
cfg.saveConfig();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -