在开发应用程序过程中,为了移植方便,我们常常将一些公用的东西写到xml文件中进行配置,比如:连接数据库的一些参数,需要读取的文件路径,常用表名称等等。那么怎样从xml文件中读取和写入这些内容呢?下边介绍一下用SAXReader类的操作
(本文感谢好朋友贺永明的帮助)
XML文件操作类:
public class XmlOperate
{
public final static Logger log = Logger.getLogger(XmlOperate.class);
/**
*
* @param d
* 要写入的document
* @param filePath
* @param fileName
* @param encoding
* @param type
* @return
*/
public static boolean writerDocument(Document doc, String fileName,
String encoding)
{
try
{
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(encoding);
File sFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(sFile);
XMLWriter writer = new XMLWriter(fos, format);
writer.write(doc);
writer.close();
fos.close();
return true;
}
catch (IOException e)
{
log.error(e);
}
return false;
}
/**
*
* @param fileName
* 配置文件名称
* @return 配置文件对应的document
*/
public static Document getDocumentTree(String fileName)
{
Document doc = null;
try
{
File f = new File(fileName);
SAXReader reader = new SAXReader();
doc = reader.read(f);
return doc;
}
catch (DocumentException e)
{
log.error(e);
e.printStackTrace();
}
return doc;
}
/**
* 获取将要生成哦初始化配置
*
* @param filePath
* 文件路径
* @param fileName
* 文件名
* @return 返回Element
*/
public static Element getInitElement(String filePath, String fileName) {
Document doc = null;
Element root;
String fullName;
if (!"".equals(filePath)) {
fullName = filePath + File.separatorChar + fileName;
} else {
fullName = fileName;
}
SAXReader reader = new SAXReader();
reader.setEncoding("GB2312");
File f = new File(fullName);
try {
doc = reader.read(f);
} catch (DocumentException e) {
log.error(e);
e.printStackTrace();
}
root = doc.getRootElement();
return root;
}
}
配置信息获取类 :
public class configOperater
{
/**
* 获取数据源
* @date 2010-1-12
* @time 上午09:23:36
* @param fileName
* @return
*/
public static DataSourceBean getDatasourceBean(String fileName)
{
Document doc = XmlOperate.getDocumentTree(fileName);
Element root = doc.getRootElement();
Element ds = root.element("datasource");
DataSourceBean dsb = new DataSourceBean();
dsb.setDataSourceName(ds.elementText("name"));
dsb.setDescription(ds.elementText("connectionDesciption"));
dsb.setDriver(ds.elementText("connectDriver"));
dsb.setDriverModel(ds.elementText("driverModel"));
dsb.setPassword(ds.elementText("connectionPassword"));
dsb.setSchema(ds.elementText("connectionSchema"));
dsb.setUrl(ds.elementText("connectURL"));
dsb.setUserName(ds.elementText("connectionUsername"));
return dsb;
}
/**
* 获取配置信息
* @date 2010-1-12
* @time 上午09:23:41
* @param fileName 配置文件名称
* @param tagName 配置文件标签
* @return
*/
public static String getConfigMessage(String fileName ,String tagName)
{
String mes="";
Document doc = XmlOperate.getDocumentTree(fileName);
Element root = doc.getRootElement();
mes = root.elementText(tagName);
return mes;
}
}
配置文件为:config.xml
<config>
<projectName>E文件</projectName>
<dqID>4</dqID>
<dqBM>CZ</dqBM>
<waitTime>1200</waitTime>
<writeXmlTime>30</writeXmlTime>
<tags>system,ValueInfo,YxValueInfo,</tags>
<realFilePath>D:/scadaDataFtp/czReal</realFilePath>
<historyFilePath>D:/scadaDataFtp/czHistory</historyFilePath>
<realTableName>czreal</realTableName>
<datasource>
<name>hbPGSIS_MONITOR</name>
<driverModel>Oracle(thin)</driverModel>
<connectURL>jdbc:oracle:thin:@127.0.0.1:1521:IMSBASE</connectURL>
<connectDriver>oracle.jdbc.driver.OracleDriver</connectDriver>
<connectionUsername>hbpgmis_monitor</connectionUsername>
<connectionPassword>hbpgmis_monitor</connectionPassword>
<connectionSchema>hbpgmis_monitor</connectionSchema>
<connectionDesciption>hbpgmis_monitor</connectionDesciption>
</datasource>
</config>
其中<config>节点下的子节点可以直接获取到节点的内容。
<datasource>下的节点内容是单独获取的。这里只是简单的将我在项目中用过的文件作例子,读者可以举一反三,自己写文件测试
测试类
public class Test
{
public static void main(String [] args){
// 获取节点 <projectName> 的内容
String str = null;
str = configOperater.getConfigMessage("config.xml","projectName");
System.out.println("str:"+str);
}
}
控制台显示:
str:E文件