import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
/**
* The class is used to parse config file(eg: *.xml file, *.properties file).
* This class is a singleton class, you can use the method newInstance to get
* an instance.
*
* @author Everest 07/09/2009
* @since 1.0
*
*/
public class ConfigUtil {
/** the default xml file to initializer */
private static final String DEFAULT_XML = "everest.xml";
private FileConfiguration config = null;
private static ConfigUtil instance = null;
private ConfigUtil(String configFile) throws ConfigurationException {
if ((config == null) && (configFile.toLowerCase().endsWith(".xml"))) {
config = new XMLConfiguration(configFile);
} else if (configFile.toLowerCase().endsWith("properties")) {
config = new PropertiesConfiguration(configFile);
} else {
config = new XMLConfiguration(DEFAULT_XML);
}
config.setReloadingStrategy(new FileChangedReloadingStrategy());
}
/**
* The method is used to get an instance of ConfigUtil
*
* @param configFile the file will be parsed
* @return An instance of ConfigUtil
* @throws Exception
*/
public static ConfigUtil newInstance(String configFile) throws Exception {
if (instance == null) {
try {
instance = new ConfigUtil(configFile);
} catch (ConfigurationException e) {
throw new Exception("The file:[" + configFile
+ "] can't be parsed", e);
}
}
return instance;
}
/**
* The method is used to return a single value
*
* @param exp what you want to get(XPath Expression)
* @return
*/
public String selectValue(String exp) {
return config.getString(exp);
}
/**
*
* @param exp
* @return
*/
public String[] selectValues(String exp) {
return config.getStringArray(exp);
}
/**public static void main(String[] args) {
try {
ConfigUtil cu = newInstance("everest.xml");
String serv = cu.selectValue("server.ip");
String ip = cu.selectValue("server.port");
System.out.println(serv+":"+ip);
} catch (Exception e) {
e.printStackTrace();
}
}*/
}
示例xml文件:everest.xml
<?xml version="1.0" encoding="utf-8"?>
<everest>
<server>
<ip>192.168.0.1</ip>
<port>8080</port>
</server>
</everest>
注:附件中是需要的相关jar包,需要加到项目的配置路径下
分享到:
相关推荐
一个项目同时用dom解析和sax解析xml文件貌似会报错,项目框架建一直是用sax和dom4j解析xml文件的。当我用dom解析xml文件。导入包后就报错识别不了xml文件的编码格式。于是做了一个sax解析xml文件的实例
通过dom4j解析xml,修改xml,自动生成xml文件。使用dom4j模拟解析web.xml配置文件,同时考虑到一个url-pattern和多个url-pattern的情况。
java在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava在线解析xmljava...
C++实现对xml文件的解析,C++实现对xml文件的解析,C++实现对xml文件的解析。
支持SAX解析XML文件,DOM解析XML文件,Pull解析器解析XML文件,Pull生成XML文件,JAVA和Kotlin两种语言编写
java解析XML文件java解析XML文件java解析XML文件java解析XML文件java解析XML文件java解析XML文件
python解析xml文件,python语言解析xml文件的常用的有两种方式: 通过MiniDom库解析xml文件 通过ElementTree库解析xml文件
关于解析xml文件的,很有用的东东关于解析xml文件的,很有用的东东关于解析xml文件的,很有用的东东关于解析xml文件的,很有用的东东关于解析xml文件的,很有用的东东关于解析xml文件的,很有用的东东关于解析xml...
解析xml解析xml解析xml解析xml解析xml解析xml解析xml解析xml解析xml解析xml解析xml解析xml解析xml解析xml
JAVA高手解析XML配置文件的读取操作.pdf
kettle 解析xml数据,xml多层分组嵌套,xml stax方法,完整解析案例使用(包含xml文件以及ktr文件)。ETL大数据迁移,数据清洗。XML Input Stream (StAX) 方法
提供对Xml文件的解析功能,xml, parser
MFC 解析XML类和Markup解析XML文件实例
用于解析XML文件用于解析XML文件用于解析XML文件用于解析XML文件用于解析XML文件用于解析XML文件用于解析XML文件用于解析XML文件用于解析XML文件
JAVA 解析XML和生成XML文档源码。比较全 1.DOM生成和解析XML文档 2.SAX生成和解析XML文档 3.DOM4J生成和解析XML文档 4.JDOM生成和解析XML
关于jq读取xml的,有需要的朋友可以下载。
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。 TinyXML是一个开源的解析XML的解析库,能够用于C++,...
tinyxml解析XML文件
android下解析xml文件的demo