The reading process board of the XML configuration file is a gold combination. There are many articles on the Internet. XML is exchanged as an e-commerce, which already has an irreplaceable role, but in the usual system In the development, we don't necessarily use data exchange, is it unable to use XML? Of course, there is already a new trend now, the Java program has the same XML format, which is used to use a Windows in INI format. Java also has a class such a class specifically handles such an attribute profile). Use XML as a profile of Java, there are many benefits, from Tomcat installation configuration files and J2EE configuration files, we have seen XML's universal application, let We also follow the trends with XML armed with XML. How is the key to read XML configuration files? There are several XML parsers: mainly DOM and SAX, these differences online article introduction. In the XML project group of Apache, current There are Xerces Xalan Cocoon's Project.Tomcat, which is developed by XML related technologies itself, is used by Sun's JAXP, and the Xerces parser in XSL Taglib Project is used. Ok, above is a more annoying theoretical issue, or cut into XML. Configuration file read. In our program, there is usually some variables determined according to the host environment. For example, the database access username and password, different hosts may set differently. As long as you change the XML configuration file, you can run it normally. localhost dbhost> SQLNAME dbname> UserName dbuser> password dbpassword> datasource> myenv> above this Myenv. XML configuration files are typically placed in Tomcat's web-inf / class. We compile a Java program to read directly, extract DBHOST DBUSER DBPASSWORD for other programs to access databases. Currently with SAX comparison, mainly different from DOM Is SAX is a row and a line of reading XML files for analysis, suitable for more large files, DOM is reading memory at one time, obviously cannot deal with big files. Here we use SAX resolution, because SAX parsers are constantly developing, there are many articles on the Internet It is for the old version. If you use JDK1.4, you can refer to the text using SAX to process the XML document. The program is based on its improvement and has been commissioned. Java programs read on the above Myenv.xml: import org.xml.sax.attributes; import org.xml.sax.helpers.defaulthandler; import org.xml.sax.saxexception; import java.util.properties; // the advantage of using it is not necessary DefaultHandler all display methods, public class ConfigParser extends DefaultHandler {Properties for storing values defining a private Properties props dbhost dbuser dbpassword of; private String currentSet; private String currentName; private StringBuffer currentValue = new StringBuffer (); / / Builder Initialization PropSpublic configParser () {this.props = new profment ();} public property getProps () {Return this.props;} // Define how to start resolving elements. Here is
The extracted name xxx .public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {currentValue.delete (0, currentValue.length ()); this.currentName = qName;} // This is the Add the value between xxx> to CurrentValuepublic void character {currentValue.Append (CH, Start, Length);} // encountered in the "CHAR [] CH, INT Start, Int Length. / xxx> After the end, the previous name and value are saved in the PUBLIC VOID Endelement (String Uri, String Localname, String Qname) throws SaxException {Props.Put (QName.tolowerCase (), currentValue.tostring () .trim ());}} The above parser is simpler? In fact, the XML is so simple. Now we have extracted DBHOST DBUSER DBPassword's value localhost sqlname usrname password. But this is just in the parser, Our programs cannot be accessed. Need to prepare a program. IMPORT JAVA.UTIL.PROPERTIES; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import java.net.url; public class parsexml {/ / Define a Properties used to store DBHOST DBUSER DBPASSWORD Value Properties Props; // Here's PROPSPUBLIC Properties getProps () {Return this.props;} public void Parse (String FileName) Throws E xception {// parser object of our ConfigParser handler = new ConfigParser (); // Get SAX factory object SAXParserFactory factory = SAXParserFactory.newInstance (); factory.setNamespaceAware (false); factory.setValidating (false); // Get SAX parsing SAXPARSER PARSER = factory.newsaxparser (); // Get the directory where the configuration file Myenv.xml is located. Tomcat is in the web-inf / class // below, beansconStants is used to store configuration information in the XML file, You can replace or define url confur = beansconstants.class.getclassLoader (). GetResource (filename); try {// Links the parser and parsing object MYENV.XML, start parsing Parser.Parse (), handler ); // After obtaining the successful attribute after resolution, other applications can extract the property name and value prOPS = handler.getProps ();} finally {factory = null; parser = null; handler = NULL;