In a recent MIS project, to avoid hard coding, I need to write some configuration information in a configuration file. Considering that it is a J2EE project, the J2EE configuration file
It seems that they are all xml files. Is it a little outdated to use traditional INI files?
OK. use xml as the configuration file.
My configuration file reportenv. xml is as follows, which is relatively simple:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Reportenv>
<Datasource>
<Username> sqlname </username>
<Password> password </password>
</Datasource>
</Reportenv>
The question is, what can I use to read configuration information?
Currently, dom4j and sax are popular. I used to use dom4j all the time. However, weblogic workshop comes with sax. I don't want to introduce any more packages, so it's just sax.
Step 1: ConfigParser. java
/*
* Create Date: 2005-6-13
* Create By: Banqiao Renren
* Purpose: xml configuration file property Reader
*/
Package com. infoearth. report;
Import org. xml. sax. Attributes;
Import org. xml. sax. helpers. DefaultHandler;
Import org. xml. sax. SAXException;
Import java. util. Properties;
Public class ConfigParser extends DefaultHandler {
/// Define a Properties to store the property value
Private Properties props;
Private String currentSet;
Private String currentName;
Private StringBuffer currentValue = new StringBuffer ();
// The Builder initializes props.
Public ConfigParser (){
This. props = new Properties ();
}
Public Properties getProps (){
Return this. props;
}
// Define the method for starting element parsing. Here, the name xxx in <xxx> is extracted.
Public void startElement (String uri, String localName, String qName, Attributes attributes)
Throws SAXException {
CurrentValue. delete (0, currentValue. length ());